2.2. Template variables
-
Install BCCMS and follow the Quickstart to set up your module and first panel template
-
For variables create a folder "definitions" under your module folder.
-
Create definition file for the panel. This has to be json file with the same name as panel. Eg c://xampp/htdocs/myproject/modules/myproject/definitions/myfirst.json
-
Let's add a text variable to the template, so you can say various greetings to the different things. For this the file should contain:
{
"version":"2",
"item":[
{
"type":"text",
"name":"greeting",
"label":"Greeting",
"default":"Hello"
},
{
"type":"text",
"name":"thing",
"label":"Thing",
"default":"World"
}
]
}
version - definition file format (has to be 2) item - array of fields on template type - field type text (check "CMS input types" for options) name - variable name (lowercase, numbers-letters only, starts with a letter) label - label for variable what you want to show in CMS admin side default - default value for variable
- Add variables to template. Edit panel template file c://xampp/htdocs/myproject/modules/myproject/templates/myfirst.tpl.php
Make this contain this:
<?= $greeting ?> <?= $thing ?>!
<?=
and ?>
are markup for PHP templating subset, which print out the variable.
As you see, the variables defined in json file, will appear here as PHP variables.
- Go to CMS and Pages -> Homepage -> Myfirst (the panel you added in Quickstart, appears in the right column)
- Modify the Greeting and the Thing fields.
- Click Save
-
Check your website frontend at http://localhost/myproject/
-
Add another panel of the same type to the page in the CMS with different content.
-
Try to rearrange panels by drag-and-drop in the CMS page edit.