Files
nextcloud-docs/developer_manual/basics/front-end/templates.rst
Christoph Wurst 2647cd93c7 Restructure the dev manual
This moves lots of pages around. The high-level changes are

* Better main sections, so it's more *general*, *into*, *basics* and *details*
* Move more general topics to a *Basic* section, which are not
  app-specific
* Remove app docs to the stuff that is likely used, anything else goes
  into "Digging deeper"
* Move general guides into a prologue
* Try to *compress*/combine some pages with similar content
* Try to have better consistencs on level ob abstraction across pages
* Split app development and maintenance pages into two sections
* Integrate bugtracker info into prologue
* Integrate Android pages into client APIs section

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2020-06-22 18:35:59 +02:00

71 lines
2.0 KiB
ReStructuredText

=========
Templates
=========
.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>
Nextcloud provides its own templating system which is basically plain PHP with some additional functions and preset variables. All the parameters which have been passed from the :doc:`controller <../requests/controllers>` are available in an array called **$_[]**, e.g.::
array('key' => 'something')
can be accessed through::
$_['key']
.. note:: To prevent XSS the following PHP **functions for printing are forbidden: echo, print() and <?=**. Instead use the **p()** function for printing your values. Should you require unescaped printing, **double check for XSS** and use: :php:func:`print_unescaped`.
Printing values is done by using the **p()** function, printing HTML is done by using **print_unescaped()**
:file:`templates/main.php`
.. code-block:: php
<?php foreach($_['entries'] as $entry){ ?>
<p><?php p($entry); ?></p>
<?php
}
Including templates
-------------------
Templates can also include other templates by using the **$this->inc('templateName')** method.
.. code-block:: php
<?php print_unescaped($this->inc('sub.inc')); ?>
The parent variables will also be available in the included templates, but should you require it, you can also pass new variables to it by using the second optional parameter as array for **$this->inc**.
:file:`templates/sub.inc.php`
.. code-block:: php
<div>I am included, but I can still access the parents variables!</div>
<?php p($_['name']); ?>
<?php print_unescaped($this->inc('other_template', array('variable' => 'value'))); ?>
Including CSS and JavaScript
----------------------------
To include CSS or JavaScript use the **style** and **script** functions:
.. code-block:: php
<?php
script('myapp', 'script'); // add js/script.js
style('myapp', 'style'); // add css/style.css
Including images
----------------
To generate links to images use the **image_path** function:
.. code-block:: php
<img src="<?php print_unescaped(image_path('myapp', 'app.png')); ?>" />