About Features Downloads Getting Started Documentation Events Support GitHub

Love VuFind®? Consider becoming a financial supporter. Your support helps build a better VuFind®!

Site Tools


Warning: This page has not been updated in over over a year and may be outdated or deprecated.
development:plugins:view_helpers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
development:plugins:view_helpers [2015/12/10 19:13] – ↷ Page moved and renamed from vufind2:building_a_view_helper to development:plugins:view_helpers demiankatzdevelopment:plugins:view_helpers [2020/03/19 21:53] (current) – [Building a View Helper] crhallberg
Line 1: Line 1:
-====== Building a View Helper ======+====== View Helpers ======
  
-View helpers are a convenient way of encapsulating logic that is useful during template rendering.  All registered view helpers are accessible as methods of $this within templates. See the [[http://framework.zend.com/manual/current/en/modules/zend.view.helpers.html|Zend Framework manual]] for more general information. The notes below account for some VuFind-specific details (since VuFind's theme system makes some details slightly different than in a "vanilla" ZF application).+===== Building a View Helper ===== 
 + 
 +View helpers are a convenient way of encapsulating logic that is useful during template rendering.  All registered view helpers are accessible as methods of $this within templates. See the [[https://docs.laminas.dev/laminas-view/helpers/intro/|Laminas manual]] for more general information. The notes below account for some VuFind-specific details (since VuFind's theme system makes some details slightly different than in a "vanilla" Laminas application).
  
 Creating a view helper is simple: Creating a view helper is simple:
  
-  * Pick a namespace for your view helpers.  This should usually be inside a [[customizing_vufind_2.0#modules|custom module]]. +  * Pick a namespace for your view helpers.  This should usually be inside a [[development:architecture:customizing_vufind#modules|custom module]]. 
-  * In your chosen namespace, create a class that extends \Zend\View\Helper\AbstractHelper.  You can either implement all of your functionality in the class's <nowiki>__invoke()</nowiki> method (in which case you can use the helper with calls like <nowiki>$this->myHelper()</nowiki>) or else you can leave <nowiki>__invoke()</nowiki> out and add a number of public methods (in which case you can use the helper with calls like <nowiki>$this->myHelper()->myMethod()</nowiki>). +  * In your chosen namespace, create a class that extends \Laminas\View\Helper\AbstractHelper.  You can either implement all of your functionality in the class's <nowiki>__invoke()</nowiki> method (in which case you can use the helper with calls like <nowiki>$this->myHelper()</nowiki>) or else you can leave <nowiki>__invoke()</nowiki> out and add a number of public methods (in which case you can use the helper with calls like <nowiki>$this->myHelper()->myMethod()</nowiki>). 
-  * Create a [[customizing the user interface|custom theme]] if you have not done so already.   +  * Create a [[development:architecture:user_interface|custom theme]] if you have not done so already.   
-  * Configure the new helper in the 'helpers' section of the theme.config.php file for your current theme.  This is a standard [[http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html|Zend Framework 2 service manager]] configuration with invokables, factories, etc.+  * Configure the new helper in the 'helpers' section of the theme.config.php file for your current theme.  This is a standard [[https://docs.laminas.dev/laminas-servicemanager/quick-start/|Laminas service manager]] configuration with invokables, factories, etc
 + 
 +===== Commonly Used Helpers ===== 
 + 
 +==== Slots ==== 
 + 
 +Slots are designed to be the building blocks on interfaces. Their main feature is that they are not overridden once set, meaning you can set some customizations and be sure that they will be displayed in the interface. Slots can hold any kind of data and can also capture buffers to be displayed later. Here are a few examples. 
 + 
 +<code php> 
 +$this->slot('demo')->set('one'); 
 +$this->slot('demo', 'two'); // set shortcut, not saved since 'one' is already set 
 +     
 +echo $this->slot('demo')->get(); // 'one' 
 +     
 +echo $this->slot('unset')->get('two'); // you can pass a default that returns if slot is empty 
 +</code> 
 + 
 +<code php> 
 +// --- custom file --- 
 +<?php $this->slot('demo')->start(); // begin a buffer capture ?> 
 +    <h2>Heading</h2> 
 +    <p>Customization</p> 
 +<?php $this->slot('demo')->end(); ?> 
 + 
 +<?php include $this->parentTemplate('footer.phtml'); ?> // pull a template from a parent theme 
 + 
 +// --- footer.phtml --- 
 +<?php $this->slot('demo')->start(); ?> 
 +    <p>Ignored default 
 +<?php echo this->slot('demo')->end(); ?> // all slot actions return the slot's contents for easy display 
 +</code> 
 + 
 +^ Method      ^ Description (all return slot contents)      ^ 
 +| set($value) | Save a value, if it's the first value written | 
 +| get([$default]) | Get the contents of a slot or a default if the slot is empty | 
 +| append($value) | Add a string to the end of a slot and after all previous appends | 
 +| prepend($value) | Add a string to the start of a slot and before all previous appends | 
 +| clear() | Empty a slot and return the previous contents | 
 +| start()   | Start buffer capture to be saved in slot | 
 +| end([$method]) | Ends a buffer capture. Method can be 'SET' (default), 'APPEND', or 'PREPEND'
 + 
 +==== appendScript/appendStylesheet ==== 
 + 
 +When you want to add robust functionality to a page without loading lots of JS and CSS on every other page, headScript and headLink are the best way to go about it.
  
 +<code php>
 +$this->headLink()->appendStylesheet('vendor/bootstrap-rtl.min.css');
 +$this->headScript()->appendFile("cart.js");
 +</code>
 ---- struct data ---- ---- struct data ----
 +properties.Page Owner : 
 ---- ----
  
development/plugins/view_helpers.1449774804.txt.gz · Last modified: 2015/12/10 19:13 by demiankatz