public
Last active

TwigView for CakePHP

  • Download Gist
default.tpl
Smarty
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
{{ html.charset() }}
<title>{{ 'CakePHP: the rapid development php framework'|trans }}: {{ title_for_layout }}</title>
{{ html.meta('icon') }}
{{ html.css('cake.generic') }}
{{ scripts_for_layout }}
</head>
<body>
<div id="container">
<div id="header">
<h1>{{
html.link('CakePHP: the rapid development php framework'|trans, 'http://cakephp.org')
}}</h1>
</div>
<div id="content">
{{ session.flash() }}{{ session.flash('auth') }}
{{ content_for_layout }}
</div>
<div id="footer">
{{
html.image('cake.power.gif', [
'alt': 'Powered by CakePHP'|trans,
'url': 'http://cakephp.org'
])
}}
</div>
</div>
</body>
</html>
example.tpl
Smarty
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<h2>Just some Twig template</h2>
 
<p>{{ html.link('text', '#something') }}</p>
 
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.</p>
 
{{ form.create('Account') }}
{{ form.input('email') }}
{{ form.input('username') }}
{{
form.input('password', [
'label':'Please enter your password'
])
}}
{{
form.input('option_test', [
'options':[
'red': 'Red Option',
'blue': 'Blue Option',
'green': 'Green Option'
]
])
}}
{{ form.end('Login') }}
twig.php
PHP
1 2 3 4 5
/**
* Converted to plugin repo
*
* @see http://github.com/m3nt0r/cakephp-twig-view
*/
using_elements.tpl
Smarty
1 2 3 4 5 6 7 8 9 10 11
<h2>Elements with Twig</h2>
 
<p>Twig has it's very own "include" command. You can still use it, but it requires a full path relative to APP/views.</p>
 
<p>To make transition easier i've added a "element" command that looks in APP/views/elements and does not require the .tpl extension"</p>
 
<div id="something">
{{ element "my_element" }}
</div>
 
<p>The 'div#something' now has the contents of APP/views/elements/my_element.tpl</p>
with_i18n.tpl
Smarty
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<h2>Login</h2>
 
{{ form.create('Account') }}
{{
form.input('username', [
'label':'Your Username'|trans
])
}}
{{
form.input('password', [
'label':'Your Password'|trans
])
}}
{{
form.input('rememberMe', [
'label':'Remember for 2 weeks'|trans,
'type':'checkbox'
])
}}
{{ form.end('Log in now'|trans) }}
 
<p>Of course the trans block does work aswell. Just like the filter it pipes the contents to the CakePHP i18n functions "__()"</p>
 
{% trans %}
Hello {{ username }}!
 
This is a long translation block.
{% endtrans %}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.