Wiki
Clone wikiAnano 2 / Templates
Templates
Anano has a simple templating engine inspired by Laravel's Blade and .NET's Razor.
Usage
To use Anano's templating, place all view files inside the app/views directory and load them with the Anano\View
(by default aliased to simply View
) class constructor, or the make
static method.
For example
#!php function getIndex() { //return View::make('home'); does the same return new View('home'); }
Anano templates should contain ordinary HTML, augmented with as many or few as you want of the following special commands.
Other responses
Sometimes you need to return something other than a view, such as JSON data. Anano has you covered with a stripped down equivalent of Symfony's Response object.
From any controller you can return Response::json( $data_array );
and similar for Response::text( $text )
, Response::download( $data, $filename )
, etc. Content types will be automatically set for you, and profiler output, if on, will be disabled.
Echoing content
#!php <div class="{{ $test ? 'class1' : 'class2' }}"> {{ $variable }} </div>
Inline conditionals etc.
#!php @if($variable): <div>True</div> @endif
These will work with any standard PHP code, including while:...endwhile, for:...endfor etc.
Specials
Root directory
Often times, especially when using AJAX, you will need to get the app's root folder. This too is easy with templates. You can retrieve it anywhere in a document with {{ App::root() }}
or use the handy shortcut @approot
directly in the URL, i.e. url: '@approot/method/arg'
.
For external Javascript files containing AJAX, the best practice is usually to set a global variable in a <script>
section in your view to the root path.
Updated