Symfony View Cheat Sheet

ADVERTISEMENT

Open-Source PHP5 MVC Framework
Agile Development
VIEW
PARTIALS, COMPONENTS, SLOTS and COMPONENT SLOTS
PARTIALS
SLOTS
Reusable chunk of template code. A template can include partials whether
A placeholder that you can put in any of the view elements (in the layout,
it is in the same module, in another module, or in the global templates/
a template, or a partial). Filling this placeholder is just like setting a
directory. Partials have access to the usual symfony helpers and template
variable. The filling code is stored globally in the response, so you can
shorcuts, but not to the variables defined in the action calling it, unless
define it anywhere (in the layout, a template, or a partial). Slots are very
passed explicitly as an argument.
useful to define zones meant to display contextual content.
Template
Layout
Layout
Article Detail
Best Articles
Lastest Articles
Slot
Slot
Article Partial
Article Partial
Article Partial
Template
=
Template
+
1
1
Article Partial
Article Partial
Slot 1
Slot 2
Slot 2
Article Partial
Article Partial
Slot 2
USING A PARTIAL
USING A SLOT
C
reate a file named
_<partial_name>.php
,
that contains the parti
al, in:
To define a slot in a template:
<myproject>/apps/<myapp>/modules/<mymodule>/templates/
Each template has the ability to define the contents of a slot.
As slots are meant to hold HTML code, just write the slot code between
To include a partial:
a call to the slot(<slot_name>) and end_slot() helpers.
<?php include_partial('<module>/<partial_name>’, array('<var>'=>$<var>)) ?>
E.g.: Overriding the 'sidebar' slot content in a template
Global partials:
<?php slot('sidebar') ?>
<!-- custom sidebar code for the current template-->
S
hould be in
:
<myproject>/apps/<myapp>/templates/
<h1>User details</h1>
To include a global partial:
<p>name: <?php echo $user->getName() ?></p>
<?php include_partial('global/<partial_name>’) ?>
<p>email: <?php echo $user->getEmail() ?></p>
<?php end_slot() ?>
To include a slot:
COMPONENTS
<?php include_slot('<slot_name>’) ?>
A partial with a logic behind.
Template
Is like an action, it can pass
To verify if a slot is defined:
Headlines Sidebar
variables to a template
<?php has_slot('<slot_name>’) ?>
partial, except it's much
The has_slot() helper returns true if the slot has been defined before,
faster. The logic is kept in a
Headlines Component
Logic
providing a fallback mechanism.
components.class.php file
in the actions/ directory,
E.g.: Including a 'sidebar' slot in the layout
Headlines Partial
Presentation
and the template is a regular partial.
<div id="sidebar">
You can include components in components,
<?php if (has_slot('sidebar')): ?>
or in the global layout, as in any regular template.
<?php include_slot('sidebar') ?>
<?php else: ?>
USING A COMPONENT
<!-- default sidebar code -->
Presentation:
<h1>Contextual zone</h1>
C
reate a file named
_<component_name>.php
, in:
<p>This zone contains links and information relative to the
main content of the page.</p>
<myproject>/apps/<myapp>/modules/<mymodule>/templates/
<?php endif; ?>
...
</div>
<?php foreach($news as $headline): ?>
<li>
<?php echo $headline->getPublishedAt() ?>
<?php echo link_to($headline->getTitle(),'news/show?id='.$headline->getId()) ?>
</li>
COMPONENT SLOTS
<?php endforeach ?>
...
A component which varies according to the module calling it. Component
slots are named placeholders that you can declare in the view elements.
Logic:
For a component slot, the code results from the execution of a component
C
reate a file named
components.class.php
,
that is the
class inheriting
and the name of this component comes from the view configuration.
from sfComponents,
in:
Useful for breadcrumbs, contextual navigations and dynamic insertions.
<myproject>/apps/<myapp>/modules/<mymodule>/actions/
<?php
E.g.:
USING A COMPONENT SLOT
class
newsComponents
extends
sfComponents
{
public function
executeHeadline()
{
To set a component slot placeholder:
$c = new Criteria();
<?php include_component_slot('<component_slot_name>’) ?>
$c->addDescendingOrderByColumn(NewsPeer::PUBLISHED_AT);
$c->setLimit(5);
$this->news = NewsPeer::doSelect($c);
To define a default component slot in the
view.yml
(located in
}
<myapp>/config
):
}
default:
components:
To call a component:
<component_slot_name>: [<module_name>, <partial_name>]
include_component('<module>', '<component_name>’, array('<var>'=>$<var>))
This will call the execute<partial_name> method of the
<?php include_component('news', 'headlines ) ?>
'
<module_name>Components class in the components.class.php located
Components accept additional parameters in the shape of an associative
in
<module_name> module,
and will display the _<partial_name>.php
array. The parameters are available to the partial under their name, and
located in:
in the component via the $this object:
<myproject>/apps/<myapp>/modules/<module_name>/templates/
E.g.:
call to the component:
To disable a component slot in
view.yml
(located in
<myapp>/config
):
<?php include_component('news', 'headlines', array('foo' => 'bar')) ?>
in the component itself:
all:
<?php echo $this->foo; ?>
// 'bar'
components:
in the _headlines.php partial:
<component_slot_name>: []
<?php echo $foo; ?>
// 'bar’
This cheat-sheet is not an official part of the symfony documentation

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go