vierwd / typo3-smarty
Use Smarty in Templates instead of Fluid
Installs: 4 475
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 5
Forks: 0
Open Issues: 0
Type:typo3-cms-extension
Requires
- php: >=7.2.0
- smarty/smarty: ^3.1, !=3.1.30
- typo3/cms-core: ^10.4.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.0
- friendsofphp/php-cs-fixer: ^2.12.0
- nimut/testing-framework: ^5.0.0
- phpstan/extension-installer: ^1.0
- phpunit/phpunit: ^6.0
- saschaegerer/phpstan-typo3: ^0.13.1
- vierwd/coding-standard: dev-master
Replaces
- vierwd/vierwd_smarty: 10.2.0
- dev-main / 13.x-dev
- 12.4.x-dev
- 11.5.x-dev
- 10.4.x-dev
- 10.2.0
- 10.1.0
- 10.0.0
- 9.5.x-dev
- 3.3.1
- 3.3.0
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.x-dev
- 2.11.0
- 2.10.3
- 2.10.2
- 2.10.1
- 2.10.0
- 2.9.2
- 2.9.1
- 2.9.0
- 2.8.0
- 2.7.10
- 2.7.9
- 2.7.8
- 2.7.7
- 2.7.6
- 2.7.5
- 2.7.4
- 2.7.3
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.4
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.6
- 2.5.5
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.3
- 2.3.2
- 2.3.0
- 2.2.1
- 2.2.0
This package is auto-updated.
Last update: 2024-12-04 09:28:27 UTC
README
Use Smarty in your templates and extbase extensions.
Installation
Install using composer:
composer require 'vierwd/typo3-smarty'
Usage in controllers
To use smarty templates for your extension's actions, just extend the Vierwd\VierwdSmarty\Controller\ActionController
. Your templates need to be at the same location as your Fluid templates used to be, but with the file extension .tpl
.
Example
// Classes/Controller/BlogController.php namespace Example\ExampleBlog\Controller; class BlogController extends \Vierwd\VierwdSmarty\Controller\ActionController { /** * @var \Example\ExampleBlog\Domain\Repository\PostRepository * @TYPO3\CMS\Extbase\Annotation\Inject */ protected $postRepository; public function listAction() { $posts = $this->postRepository->findAll(); $this->view->assign('posts', $posts); } }
{* Resources/Private/Templates/Blog/List.tpl *} {foreach $posts as $post} <div class="post"> <h1>{$post->getTitle()}</h1> {$post->getContent()|escape|nl2p nofilter} </div> {/foreach}
Pre-defined variables
There are some variables, that are always available to your templates:
Pre-defined smarty functions, blocks and modifiers
- translate
- uri_resource
- uri_action
- link_action
- flashMessages
- nl2p
- typolink
- typoscript
- fluid
- svg
Power-Blocks: typoscript and fluid
What's realy good about this extension is, that you can still use typoscript and fluid within your Smarty templates. That way you can ensure, that an element gets exactly the same HTML output as a normal content element like Text-with-images. If you write a form, it's also good to fallback to Fluid for some ViewHelpers.
Typoscript
{capture assign=text} <p>Lorem <b>ipsum</b> dolor sit amet, consectetur adipisicing elit. Dolorem, earum est reiciendis modi neque in veniam rerum deleniti et praesentium? Numquam, odit, itaque voluptate pariatur adipisci enim tempora ducimus dolor!</p> {/capture} {typoscript header='TypoScript Example' bodytext=$text CType=text} lib.parseFunc_RTE > 10 < tt_content {/typoscript}
Output (Line-breaks added)
<div class="csc-default"> <header class="csc-header"><h1>TypoScript Example</h1></header> <p>Lorem <b>ipsum</b> dolor sit amet, consectetur adipisicing elit. Dolorem, earum est reiciendis modi neque in veniam rerum deleniti et praesentium? Numquam, odit, itaque voluptate pariatur adipisci enim tempora ducimus dolor!</p> </div>
The changes to the typoscript will not persist. That way you can remove lib.parseFunc_RTE
in one TypoScript Block and still use it in another. It is also possible to use an array data
for all arguments:
{capture assign=text} <p>Lorem <b>ipsum</b> dolor sit amet, consectetur adipisicing elit. Dolorem, earum est reiciendis modi neque in veniam rerum deleniti et praesentium? Numquam, odit, itaque voluptate pariatur adipisci enim tempora ducimus dolor!</p> {/capture} {$data=[ CType => text, header => 'TypoScript Example', header_layout => 1, bodytext => $text ]} {typoscript data=$data header_layout=2} lib.parseFunc_RTE > 10 < tt_content {/typoscript}
Notice, that parameters in the block-tag override array keys (in this example header_layout):
Output (Line-breaks added)
<div class="csc-default"> <header class="csc-header"><h2>TypoScript Example</h2></header> <p>Lorem <b>ipsum</b> dolor sit amet, consectetur adipisicing elit. Dolorem, earum est reiciendis modi neque in veniam rerum deleniti et praesentium? Numquam, odit, itaque voluptate pariatur adipisci enim tempora ducimus dolor!</p> </div>
Using Smarty for the base template
It is also possible to use Smarty for the base template of your website in your main TypoScript setup
page = PAGE
page.10 < plugin.tx_vierwdsmarty
page.10.settings {
template = EXT:example_blog/Resources/Private/Templates/main.tpl
typoscript.navigation < lib.navigation
typoscript.footerNavigation < lib.footerNavigation
typoscript.content < styles.content.get
typoscript.logo < lib.logo
}
All entries in settings.typoscript will be parsed and will be available as variables in your template.
{* example_blog/Resources/Private/Templates/main.tpl *} <header> {$logo nofilter} {$navigation nofilter} </header> <div role="main"> <!--TYPO3SEARCH_begin--> {$content nofilter} <!--TYPO3SEARCH_end--> </div> <footer>{$footerNavigation nofilter}</footer>
Note the nofilter argument for Smarty. By default all variables will be escaped to prevent some XSS attacks.
Using Smarty for Menus
lib.navigation = HMENU
lib.navigation {
entryLevel = 0
1 = SMARTY
1 {
expAll = 1
extensionName = vierwd_example
template = Navigation/Main.tpl
NO = 1
ACT = 1
IFSUB = 1
ACTIFSUB = 1
}
2 < .1
2.template = Navigation/Submenu.tpl
3 < .2
}
This code block will load the templates at typo3conf/ext/vierwd_example/Resources/Private/Templates/Navigation/
to render the navigation. Within the template you can iterate over your menu items and output the menu:
<nav class="main-navigation"> <ul> {foreach $menu as $item} {$hasSubmenu = $menuObject->isSubMenu($item.uid)} {$isActive = $menuObject->isActive($item.uid)} <li class="{if $isActive}active{/if}"> <a href="{$item.uid|typolink}"> {$item.nav_title|default:$item.title} </a> {$menuObject->submenu($item.uid) nofilter} </li> {/foreach} </ul> </nav>