-
Notifications
You must be signed in to change notification settings - Fork 7
Tutorial Templates and TVs
Packaging MODX Templates with Repoman often goes hand-in-hand with using Template Variables (TVs) and static assets (e.g. CSS, Javascript) that accompanies your Template.
This part of building your template package is pretty easy: just create an assets/ directory inside your Repoman repo and add your static assets inside that directory. You can add sub-directories inside the assets/ directory to your liking. Just make sure your templates and chunks refer to this directory by using the [[++your-pkg.assets_url]] placeholder. Repoman will strip these from your templates and chunks and replace them with the generic [[++assets_url]] placeholder when your files are packaged.
WARNING: You should never hard-code your assets url in any MODX template or chunk, regardless of whether you are using Repoman.
Your template can be a regular MODX template, we just add a Repoman DocBlock to define the @templatename and @description attributes.
When attaching TVs, note that we make use of a special @TVs attribute. The names of the TVs should match exactly the file names you define in the next section. The order of the TVs will affect the ordering of the TVs on your template.
<!--
@templatename testTemplate
@description Demonstration of a basic template utilizing static assets and TVs
@TVs moonUnitTv,dweezilTv
-->
<!DOCTYPE html>
<html>
<head>
<title>[[*pagetitle]]</title>
<meta name="description" content="[[*description]]"/>
<link rel="stylesheet" type="text/css" href="[[++templatetest.assets_url]]main.css">
</head>
<body>
<h1>[[*longtitle]]</h1>
moonUnitTv: [[*moonUnitTv]]<br/>
dweezilTv: [[*dweezilTv]]<br/>
<hr/>
[[*content]]
</body>
</html>
WARNING: DO NOT name your template files using .php. Although this is technically possible, it is considered bad practice because it is confusing and it can cause problems if your webserver attempts to optimize delivery of static content by defining rules for different file extensions. Similar problems may arise if you use a CDN such as Amazon S3.
Template Variables are more complex than the other elements: they need to define a lot of information, so they do not support any DocBlock syntax. Instead, create one PHP file for each TV inside of your elements/tvs/ directory. The file name should match the name parameter exactly -- case matters!
Contents of elements/tvs/dweezilTv.php:
<?php
return array(
'type' => 'text',
'name' => 'dweezilTv', // <-- this must match the file name exactly (minus .php)
'caption' => 'Dweezil',
'description' => 'Zap Zap Man man',
'editor_type' => 0,
'display' => '', // default
'default_text' => '',
'properties' => '',
'input_properties' => '', // serialized
'output_properties' => '', // serialized
);
/*EOF*/
Contents of elements/tvs/moonUnitTv.php:
<?php
return array(
'type' => 'text',
'name' => 'moonUnitTv', // <-- this must match the file name exactly (minus .php)
'caption' => 'Moon Unit',
'description' => 'What is your lunar unit?',
'editor_type' => 0,
'display' => '', // default
'default_text' => '',
'properties' => '',
'input_properties' => '', // serialized
'output_properties' => '', // serialized
);
/*EOF*/
When you list TVs to be included on your templates, you must reference the filename exactly (minus the .php extension).
WARNING: When running the repoman install command, TVs will generate several errors.
© 2014 and beyond by Craftsman Coding