DEPRECATION WARNING
This documentation is not using the current rendering mechanism and is probably outdated. The extension maintainer should switch to the new system. Details on how to use the rendering mechanism can be found here.
TYPO3 is Smarter¶
Created: | 2007-10-09T09:12:38 |
---|---|
Changed by: | Simon Tuck |
Changed: | 2007-10-16T18:01:51 |
Email: | stu@rtpartner.ch |
Info 2: | Simon Tuck |
Info 3: | |
Info 4: |
TYPO3 is Smarter¶
Extension Key: rtp_smarty
Copyright 2006-2007, Simon Tuck <stu@rtpartner.ch>, Rüegg Tuck Partner GmbH
This document is published under the Open Content License
available from http://www.opencontent.org/opl.shtml
The content of this document is related to TYPO3
- a GNU/GPL CMS/Framework available from www.typo3.com
Table of Contents¶
EXT: rtp_smarty 1
Introduction 1
What does it do? 1
Intended Audience 1
Why is TYPO3 Smarter? 2
Installation 2
Path to your Smarty installation 2
Path to your Smarty templates 2
Configuration 2
TypoScript Configuration 3
Direct Configuration 3
Default Configuration 3
Developers Manual 4
Step 1: Create an instance of Smarty 4
Step 2: Pass your data to Smarty 4
Step 3: Render your Smarty template 4
rtp_smarty API reference 4
Debugging Smarty Templates 4
TYPO3 Plugins 5
{translate} 5
{link} 5
{url} 6
{format} 6
{data} 7
{image} 7
Known problems 8
Changelog 8
Introduction¶
What does it do?¶
'TYPO3 is Smarter' (rtp_smarty) is an extension which integrates the Smarty templating engine into TYPO3 so that you can use Smarty as a templating engine in your extensions.Smarty ( http://smarty.php.net/ ) is a popular template language for designers and programmers.
- Smarty is fast, easy to use and well documented.
- Smarty makes it easier to split extension development between developers and designers: Developers can create the business logic while designers can tweak the presentation layer.
- Smarty offers many advanced features(Variable Modifiers, Template Functions, Filters and Plugins)for handling HTML and PHP data, making extension development easier and faster.
Intended Audience¶
This document is for extension developers who create frontend plugins using the TYPO3 framework. In addition, you should have a basic knowledge of Smarty. If you are interested in learning more about Smarty I recommend the following links:
ZEND Developer Zone: PHP Templating with Smarty
Why is TYPO3 Smarter?¶
If you're familiar with frontend extension development you'll invariably have come across the templating methods the TYPO3 framework offers for frontend extension templating: getSubpart, substituteSubpart, substituteMarker, substituteMarkerArrayCached, substituteMarkerArray, substituteMarkerInObject, fillInMarkerArray. Unfortunately these methods have a few disadvantages:
- They provide only very basic functionality, usage can be complicated and is mainly limited to replacing so called markers (for example ###MY_MARKER###) in templates. Templates tend to become large and unwieldly.
- Presentation logic has to be coded in the extension class, i.e. there is no clean separation between the business and presentation layer.
- Extension templates cannot be localized.
By contrast Smarty is a very powerful templating language which allows you to make a clean seperation between the business and the presentation layer. rtp_smarty was designed to address the deficits of the builtin frontend templating methods by integrating the Smarty library and by offering a set of customized Smarty tags and functions that are specifically geared towards frontend extension templating. rtp_smarty offers the following advantages for extension developers:
- Access to the Smarty templating engine for your extension templates.
- Works with both the traditional pi_base extension development framework and the new lib/div mvc extension development framework.
- Supports template localization.
- Supports other useful TYPO3 features such as link and image generation.
- Has a flexible plugin architecture so that you can add your own tags and functions to your templates.
- Has a debugging console for template development.
Installation¶
Install the extension with the extension manager. Once you have created the required directories, you should see a page asking you to define the location of your Smarty installation and the default location of your Smarty templates:
Path to your Smarty installation¶
The Complete Smarty package (currently version 2.6.18) is included in the extension. By default this is the Smarty installation the extension will use. If you'd prefer to use a different Smarty installation or if you have upgraded your Smarty installation to a newer version, simply change the path to your Smarty installation.
Path to your Smarty templates¶
This will define a default location for your Smarty templates. This is the path that Smarty will try to locate your Smarty templates in.
Configuration¶
You can configure Smarty in your extension's TypoScript setup or you can opt for a more direct approach and set Smarty class variables in your extension class (For a complete reference of the available Smarty class variables please refer to the online documentation at smarty.php.net ). The following example illustrate 3 different approaches to defining the Smarty class variable 'template_dir':
- Use your extension's TypoScript setup:
plugin.tx_myextension_pi1.smarty.template_dir = EXT:my_ext/templates
- Use the method 'setSmartyVar':
$smarty->setSmartyVar('template_dir','EXT:my_ext/templates');
- Set the Smarty class variable directly:
$smarty->template_dir = t3lib_div::getFileAbsFileName('EXT:my_ext/templates');
TypoScript Configuration¶
rtp_smarty will interpret any TypoScript settings of the property 'smarty' from your extension as Smarty configuration values. This means you can predefine your Smarty configuration in your extension configuration. The following example from the TypoScript setup file of an extension 'my_ext' sets the Smarty template directory and turns debugging on:
plugin.my_ext {
smarty {
template_dir = EXT:my_ext/templates
debugging = true
}
}
Direct Configuration¶
If you prefer you can set Smarty class variables directly in your extension class. If you are familiar with Smarty you will probably feel most comfortable with this approach:
$mySmartyInstance->SmartyVariable = value;
Alternatively you can use the method 'setSmartyVar'. The setSmartyVar method has the advantage that it perfoms basic sanity checks on the variable and the value you pass (for example, it will resolve TYPO3 path references if the variable requires a path). The syntax is:
$mySmartyInstance->setSmartyVar(SmartyVariable, value);
Default Configuration¶
Important: You should always include the static TypoScript template for rtp_smarty, in particular if you want to use the TYPO3 plugins. The default settings for rtp_smarty are:
plugin.rtp_smarty.¶
debugging¶
Property
debugging
Description
Enable/disable the rtp_smarty debug console.
Default
false
error_reporting¶
Property
error_reporting
Description
php error_reporting level inside of display() and fetch()
Default
E_ALL
debugging_ctrl¶
Property
debugging_ctrl
Description
Alternate ways to enable debugging. NONE means no alternate methods are allowed. URL means when the keyword SMARTY_DEBUG is found in the QUERY_STRING
Default
NONE
debug_tpl¶
Property
debug_tpl
Description
Location of the debug template
Default
EXT:rtp_smarty/debug/rtp_smarty_debug.tpl
compile_check¶
Property
compile_check
Description
Smarty tests to see if the current template has changed (different time stamp) since the last time it was compiled. This should be set to true for development and debugging purposes.
Default
true
force_compile¶
Property
force_compile
Description
Makes Smarty regenerate the template each time the script is called. Can be set to true for development and debugging purposes.
Default
false
caching¶
Property
caching
Description
Tells Smarty whether or not to cache the output of the templates.
Use of Smarty's caching mechanism inside of TYPO3 is experimental.
Default
false
plugins_dir¶
Property
plugins_dir
Description
Location of the Typo3 Smarty plugins, which provide FE functions such as localization, creating TYPO3 links etc.
Default
EXT:rtp_smarty/typo3_plugins
cache_dir¶
Property
cache_dir
Description
Default location of the Smarty cache files
Default
typo3temp/smarty_cache
compile_dir¶
Property
compile_dir
Description
Default location of the compiled Smarty template files
Default
typo3temp/smarty_compile
autoload_filters.pre.0¶
Property
autoload_filters.pre.0
autoload_filters.pre.1
Description
Array of prefilters that Smarty will autoload. These include the 'conditions' prefilter (enables using TYPO3 condition statements inside of Smarty if statements.) and the 'dots' prefilter (required for various TYPO3 Smarty plugins)
Default
conditions
dots
Developers Manual¶
Basic usage of Smarty in your extension can be summarized in three steps
Step 1: Create an instance of Smarty¶
// New instance of Smarty
$smarty = tx_rtpsmarty::smarty();
Step 2: Pass your data to Smarty¶
// Pass a variable to Smarty
$text = 'Smarty is a template language and a very useful tool for designers and programmers.'
$smarty->assign('info', $text);
// Pass an array to Smarty
$myArray = array('Apple','Banana','Mango','Strawberry');
$smarty->assign('fruit', $myArray);
Step 3: Render your Smarty template¶
// Process the template and return the parsed HTML
$content = $smarty->display('mySmartyTemplate.html');
return $content;
rtp_smarty API reference¶
::¶
Function
tx_rtpsmarty::smarty
::¶
Function
setSmartyVar
Comments
Set Smarty class variables
Method for setting Smarty configuration variables. Performs basic sanity checks when setting Smarty class variables.
Examples:
// Change the template directory
$this->smarty->setSmartyVar('template_dir','EXT_my_ext/templates');
// Enable caching
$this->smarty->setSmartyVar('caching',true);
::¶
Function
setPathToLanguageFile
Comments
Define the default location of your language file
Defines the location of the default language file. rtp_smarty will try to locate the default language file of your extension so you shouldn't need this method. If however you would like to use a different language file you should use this method:
// Define location of language file
$this->smarty->setPathToLanguageFile('fileadmin/lang/locallang.xml');
Debugging Smarty Templates¶
rtp_smarty provides a customized version of the Smarty debug console. To invoke the debug console add the debug tag to your template or configure debugging = true for your Smarty instance:
- Add the following tag to your Smarty template:
{debug}
(ii) Add the following TypoScript to your extension setup (where “tx_myextension_pi1” is your extension key):
plugin.tx_myextension_pi1.smarty.debugging = true
TYPO3 Plugins¶
Smarty has a plugin architecture which can be used to customize it's functionality (for example, by adding your won Smarty tags). rtp_smarty comes with a variety of additional functions and modifiers specifically geared towards using Smarty as a templating engine for extension development.
{translate}¶
Description¶
The translate plugin will enable you to localize your templates. Any text you place between the translate tags will be translated to the current language based on your localization file (i.e. locallang.xml). For this to work Smarty needs to know the location of your language file and the label it should reference in order to translate the text. If you are using the traditional extension framework (pi_base) the plugin will look for the language file in the pi directory of your current extension. If you are using the lib/div framework the plugin will get the current language file from your configuration (pathToLanguageFile ) . If however your language file is located elsewhere or you would like to use a different localization file than the default for your extension you need to define the location of your localization file:
Define the default language file In your extension's TypoScript configuration:
plugin.tx_myextension_pi1.smarty.pathToLanguageFile = path/to/my/localization.xml
Alternatively you can define which language file to use from within the translate tag (note that you can also use this feature to reference different language files inside a single template, so it's probably still a good idea to define a default language file):
{translate label=”LLL:path/to/my/localization.xml:the.element.label”}Some text that will be translated{/translate}
In your extension class use the method setPathToLanguageFile:
$smarty->setPathToLanguageFile('path/to/my/localization.xml');
Usage¶
label¶
Parameter
label
Description
Reference to the element label and optionally the language file to use.
Default
alt¶
Parameter
alt
Description
Provide an alternative text output if there is no translation available for the marker
Default
hsc¶
Parameter
hsc
Description
Passes the output through htmlspecialchars
Default
FALSE
Examples¶
(i) Translate text to the current language referencing the element with the label “the.element.label” in your language file:
{translate label=”the.element.label”}Some text to translate.{/translate}
(ii) Same as (i), but if the parameter “label” is missing the text between the tags is interpreted as the element label:
{translate}the.element.label{/translate}
(iii) Translate text to the current language referencing the element with the label “the.element.label” in the language file “EXT:my_ext/lang/locallang.xml”:
{translate label=”LLL:EXT:my_ext/lang/locallang.xml:the.element.label”}Some text to translate.{/translate}
{link}¶
Description¶
The link tag will create links in your template using the TYPO3 typolink function. This will result in links that are compatible with TYPO3 features such as realurl and localization etc. Furthermore the link tag can be used to resolve page ids and aliases.
Usage¶
setup¶
Parameter
setup
Description
A reference to a typoscript object in your TYPO3 template, which defines the configuration of the link, for example 'lib.myTypolink'
Default
->typolink¶
Parameter
->typolink
Description
Any property that is valid for the typolink function , for example 'parameter', 'target', 'wrap' etc.
Default
Examples¶
(i)Create a link to the page id 59:
{link parameter=”59”}About us{/link}
(ii)Create a link to an external URL which opens in a separate window and has the class “external”:
{link parameter=”http://www.google.com _blank external”}Search the web{/link}
(iii)Create an email link:
{link parameter=”info@rtpartner.ch”}Tell us what you think!{/link}
(iv)Create a file download:
{link parameter=”fileadmin/files/specs.pdf”}Further reading{/link}
(v)Create a link to the current page which has the class “print”, is wrapped in a div and has the parameter &print=1 added to the URL (Note that the keyword “_self” references the current page):
{link parameter=”_self – print” wrap=”<div>|</div>” additionalParams=”&print=1”}Print{/link}
(vi)Alternatively you can reference an existing typolink configuration from your template setup using the parameter “setup”:
{link setup=”lib.myLink”}Print{/link}
Where lib.myLink is:
lib.myLink {
parameter = 59
AtagParams = class=”board”
}
(vii)If you are using the “setup” parameter to define your link configuration you can override any setting from the link parameters. The following example will create a link to page id 60 (and not to page id 59 as defined in “lib.myLink”):
{link setup=”lib.myLink” parameter=”60”}Print{/link}
Where lib.myLink is:
lib.myLink {
parameter = 59
AtagParams = class=”board”
}
Special¶
- Note the use of the keyword '_self' in example (e). You can use this keyword to create a reference to the current page.
- If undefined the link function will use the text between the tags to create the title tag for the link.
{url}¶
Description¶
The url tag works exactly like the link tag, but it will return only the formatted url (as opposed to the complete link tag). The parameters for the url tag are the same as for the link tag. Usage of the tag varies in that there is no closing tag:
Copy this URL {url parameter=”59”} to your browser navigation toolbar.
For further details refer to the description of the link tag above.
{format}¶
Description¶
Formats a block of text (typically text from an RTE formatted content element) according to your default parseFunc configuration.
Usage¶
setup¶
Parameter
setup
Description
Reference to the parseFunc configuration used to format the text.
Default
lib.parseFunc_RTE
->parseFunc¶
Parameter
->parseFunc
Description
Any property that is valid for the parseFunc function , for example 'allowTags', 'denyTags' etc.
Default
Examples¶
(i) Parse a block of text according to the rules defined in lib.parseFunc_RTE:
{format}
Enthusiastically cultivate go forward process improvements without client-centered results. Collaboratively incentivize <LINK 23>extensive growth</LINK> strategies whereas extensible imperatives. Assertively morph innovative e-business via wireless supply chains.
Enthusiastically formulate stand-alone core competencies via backend convergence.
{/format}
- Same as (i), but use an alternate parsing configuration:
{format setup=”lib.parseFunc”}
Enthusiastically cultivate go forward process improvements without client-centered results. Collaboratively incentivize <LINK 23>extensive growth</LINK> strategies whereas extensible imperatives. Assertively morph innovative e-business via wireless supply chains.
Enthusiastically formulate stand-alone core competencies via backend convergence.
{/translate}
{data}¶
Description¶
Returns a value from the current TYPO3 data array. Could for example be used to return the current page title or a value from the register.
Usage¶
source¶
Parameter
source
Description
Reference to the data array defined as type:pointer. For a list of available type/pointer combinations refer to the getText documentation .
Default
lib.parseFunc_RTE
Examples¶
- Get the current page title:
{data source=”page:title”}
- Get the current time formatted dd-mm-yy:
{data source=”date:d-m-y”}
(iii) get the value of the header of record with uid 234 from table tt_content:
{data source=”DB:tt_content:234:header”}
{image}¶
Description¶
The image tag will render an image using the TYPO3 IMAGE function.
Usage¶
setup¶
Parameter
setup
Description
A reference to a typoscript object in your TYPO3 template, which defines the configuration of the link, for example 'lib.myTypolink'
Default
->IMAGE¶
Parameter
->IMAGE
Description
Any property that is valid for the IMAGE function , for example 'file', 'params', 'border' etc.
Default
Examples¶
(i)Render the image in fileadmin/files/myPicture.jpg:
{image file=”fileadmin/files/myPicture.jpg”}
- Same as (i) but set the width of the image to 100 pixels:
{image file=”fileadmin/files/myPicture.jpg” file.width="100"}
(iii)Render an image using a configuration defined in your TypoScript setup:
{image setup=”lib.myImage”}
Where lib.myLink is:
lib.myImage = IMAGE
file = GIFBUILDER
file {
XY = 170, [10.h]
10 = IMAGE
10.file = fileadmin/images/background.gif
20 = IMAGE
20.file = fileadmin/images/picture.jpg
20.file.width = 150
20.offset = 10,10
}
}
(iv)The same as (iii), but change the parameters of lib.myImage:
{image setup=”lib.myImage” file.20.file=”fileadmin/images/another_picture.jpg”}
Known problems¶
None so far...
Changelog¶
May, 2006:
Initial Release
October, 2007:
- Updated to the current version of Smarty (2.6.18)
- Added integration for both the pi_base and the lib/div scenario (in particular with regard to the translation plugin getLL and LLL and to the setting of the correct templates dir)
- Revised all of the plugins in the directory typo3_plugins. I have also added some new plugins and removed some which were either broken or redundant. Note: I believe the framework for the typo3_plugins is much improved now, but as a consequence the backwards compatibility of a few of the plugins is *broken*. So please be careful.
- Added a customized debug console which should work inside the Smarty fetch command and provides debug information relevant to the current Smarty template and TYPO3 extension. The debug console can be activated by setting $smarty->debugging = true or adding the tag {debug} in your template.
TYPO3 is Smarter - 8
Comments
Create a new instance of Smary
Factory API for creating instances of Smarty. Invoking tx_rtpsmarty::smarty returns a new instance of Smarty. Optionally pass an array of configuration values (Smarty class variable => configuration value) with the method to configure the Smarty instance: