search
HomeBackend DevelopmentPHP TutorialCreating a PyroCMS Theme: A Step-by-Step Guide

Like most content management systems, PyroCMS uses front-end themes. Although PyroCMS themes are built slightly differently than themes from other systems you may be used to, they are still easy to create. In fact, they're so simple that very little PHP experience is required to assemble them!


Folder structure

PyroCMS themes consist of HTML, images, CSS, and JavaScript arranged in the following supported folders:

  • CSS
  • picture
  • js
  • Times watched
  • View/Layout
  • View/Part of Content
  • View/Module

While these folders will no doubt look familiar to you, the "views" folder makes the most sense in an MVC context. When building a theme for PyroCMS, you are actually building the views (including assets) of an MVC pattern application. These views consist of a main layout file and multiple partial files (i.e. header.html or footer.html) that share presentation logic between different layouts. We'll discuss this shortly.


start using

To start building your first PyroCMS theme, create a supported folder structure in one of two locations within your PyroCMS instance:

addons/shared_addons/themes (for themes available to all sites)

or:

addons/[site-name]/themes (for themes available to only one specific site)

After creating the base theme folder with a supported folder structure, the first file you want to add to your theme is theme.php.

addons/shared_addons/themes/[my-theme-name]/theme.php

This theme.php file contains all the important details of the theme including its name, author, version, etc. In a way, this file is similar to a WordPress theme’s style.css file. Here is a basic example of a theme.php file for a PyroCMS theme:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Theme_Foo extends Theme
{
    public $name = 'Foo';
    public $author = 'Zac Vineyard';
    public $author_website = 'https://zacvineyard.com';
    public $website = 'http://example.com/themes/foo';
    public $description = 'The antithesis theme to your Bar theme.';
    public $version = '1.0';
}

 /* End of file theme.php */

Please note that this file extends a PyroCMS class named Theme. Also, since you are declaring your PHP classes in this file, you need to make sure to use the name of the folder that contains your theme in the class declaration. So, if the folder containing the theme is named "foo", the class created in theme.php should be named Theme_Foo (instead of Theme_Custom, as shown in the example in the PyroCMS documentation).

After creating the theme.php file, you can log into the PyroCMS control panel and view the themes listed in the Themes module.

创建 PyroCMS 主题:分步指南


layout

All layout files for a PyroCMS theme exist in one of two locations:

addons/[site-ref]/themes/[my-theme-name]/views/layouts/

or:

addons/shared_addons/themes/[my-theme-name]/views/layouts/

Every theme should have a layout file named "default.html" located in one of the locations listed above. Additional layout files are optional; I'll show you how to add more layout files later. First, it's important to check the contents of the layout file.

Layout files in PyroCMS are built using HTML and a tag parser (called the Lex tag parser). This is what a very basic PyroCMS layout file looks like:

<!DOCTYPE html>
<html>
<head>
    <title>{{ template:title }}</title>
    {{ template:metadata }}
</head>
<body>
    <h1 id="template-title">{{ template:title }}</h1>
    {{ template:body }}
</body>
</html>

The special tag you see in this HTML is the Lex parser tag. If you've ever used Smarty templates in PHP, these templates may look somewhat familiar. The main benefit of using Lex parser tags in your layout files is that you don't have to put PHP directly into the view (remember, we are using MVC), which gives you the ability to create patterns that follow Don't Repeat Yourself .

Of course, the example I gave above is simple, but Lex parser tags are very powerful. They can loop through data, manipulate properties, and more. Learn more about the Lex parser in the PyroCMS documentation.

A more complex PyroCMS layout file looks like this:

<!DOCTYPE html>
<html>
<head>
    <title>{{ template:title }}</title>
    {{ template:metadata }}
    {{ theme:favicon file="favicon.png" }}
    {{ theme:css file="style.css" }}
    {{ theme:js file="site.js" }}
</head>
<body>
    <div class="header">
        <div class="logo">
            {{ theme:image file="logo.jpg" alt="Your Cool Logo" }}
        </div>
        <div class="nav">
            {{ navigation:links group="header" }}
        </div>
    </div>
    <div class="content">
        <h1 id="template-title">{{ template:title }}</h1>
        {{ template:body }}
    </div>
</body>
</html>

You will notice that this layout file using Lex contains resources such as CSS, JavaScript, and images. Using Lex tags and HTML allows PyroCMS developers to build layout files very quickly.


Part

Partials in PyroCMS represent partial layouts, allowing you to break your layout into reusable parts or sections. These sections can then be loaded via different layout files. This prevents you from typing the same code (header, footer, etc.) in multiple layout files.

Depending on where you place your theme files, some content will be created in one of two locations:

addons/[site-ref]/themes/[my-theme-name]/views/partials/

or:

addons/shared_addons/themes/[my-theme-name]/views/partials/

Use this Lex tag to load some content into the layout:

{{ theme:partial name="partialname" }}

This Lex tag operates exactly like a PHP include statement - similar to what you would find in WordPress or other themes. The code below is a simple example of a partial PyroCMS layout.

{{ theme:partial name="header" }}

    <div class="content">
        <h1 id="template-title">{{ template:title }}</h1>
        {{ template:body }}
    </div>

{{ theme:partial name="footer" }}
The contents of the

header.html section and the footer.html file are of course the HTML we need to reuse from the template in the code example above. A quick tip: There is no limit to the number of sections you can use in a layout. Additionally, some files may contain any combination of valid HTML and Lex.


多个布局文件

要向 PyroCMS 实例添加另一个布局,请在主题的 views/layouts/ 目录中再创建一个布局文件。该文件可以使用任何名称,但最好尽可能具有描述性地命名 - 例如 about.html

为了增加灵活性,您可以使用任意数量的布局文件。当您在 PyroCMS 控制面板(控制面板→页面→页面类型)中编辑或创建页面类型并从下拉列表中选择所需文件时,您的所有布局主题的布局文件将可供使用。

创建 PyroCMS 主题:分步指南


移动布局

PyroCMS 能够轻松显示移动设备和桌面设备的单独布局。要使用此功能,请将布局文件移动到 views 文件夹中名为“web”的文件夹中,以便您的默认布局位于此处:

[your-theme]/views/web/layouts/default.html

当用户使用桌面浏览器访问您的网站时,将使用此位置的主要布局文件。如果用户使用移动设备浏览器访问您的网站,则会向用户提供您在此位置创建的移动布局:

[your-theme]/views/mobile/layouts/default.html

此功能适用于多个布局文件。

请注意 PyroCMS 文档中的警告:“PyroCMS 不认为 iPad 是移动设备,因此如果用户使用 iPad 访问您的网站,它不会加载您的移动布局。”但是,如果在您的网站上,您希望将 iPad 识别为移动设备,则可以更改 config/ 目录中的“user_agent.php”文件,以使 iPad 识别为移动设备。移动设备。


完成!

使用本文作为指南,您可以看到在 PyroCMS 中创建主题是多么容易。提供的代码示例非常简单,因此我鼓励您探索 PyroCMS 文档,以便对 PyroCMS 中的布局、移动布局、部分和 Lex 解析器有更丰富的经验。玩得开心!

The above is the detailed content of Creating a PyroCMS Theme: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool