mirror of
				https://github.com/smarty-php/smarty.git
				synced 2025-11-04 14:21:36 +01:00 
			
		
		
		
	
		
			
	
	
		
			128 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
		
		
			
		
	
	
			128 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
| 
								 | 
							
								<?xml version="1.0" encoding="iso-8859-1"?>
							 | 
						|||
| 
								 | 
							
								<!-- $Revision$ -->
							 | 
						|||
| 
								 | 
							
								   <sect1 id="plugins.functions"><title>Funciones de Template</title>
							 | 
						|||
| 
								 | 
							
								    <funcsynopsis>
							 | 
						|||
| 
								 | 
							
								     <funcprototype>
							 | 
						|||
| 
								 | 
							
								      <funcdef>void <function>smarty_function_<replaceable>name</replaceable></function></funcdef>
							 | 
						|||
| 
								 | 
							
								      <paramdef>array <parameter>$params</parameter></paramdef>
							 | 
						|||
| 
								 | 
							
								      <paramdef>object <parameter>&$smarty</parameter></paramdef>
							 | 
						|||
| 
								 | 
							
								     </funcprototype>
							 | 
						|||
| 
								 | 
							
								    </funcsynopsis>
							 | 
						|||
| 
								 | 
							
								    <para>
							 | 
						|||
| 
								 | 
							
								    Todos los atributos pasados para las funciones de template a partir 
							 | 
						|||
| 
								 | 
							
								    del template estan contenidas en <parameter>$params</parameter> como 
							 | 
						|||
| 
								 | 
							
								    un arreglo asociativo.
							 | 
						|||
| 
								 | 
							
								    </para>
							 | 
						|||
| 
								 | 
							
								    <para>
							 | 
						|||
| 
								 | 
							
								     La salida(valor de retorno) de la funci<63>n ser<65> substituida en
							 | 
						|||
| 
								 | 
							
								     el lugar de la etiqueta de la funci<63>n en el template (la funci<63>n
							 | 
						|||
| 
								 | 
							
								     <function>fetch</function>, por ejemplo).
							 | 
						|||
| 
								 | 
							
								     Alternativamente, la funci<63>n puede simplemente ejecutar alguna 
							 | 
						|||
| 
								 | 
							
								     otra tarea sin tener alguna salida (la funci<63>n <function>assign</function>).
							 | 
						|||
| 
								 | 
							
								    </para>
							 | 
						|||
| 
								 | 
							
								    <para>
							 | 
						|||
| 
								 | 
							
								     Si la funci<63>n necesita pasar valores a algunas variables del template 
							 | 
						|||
| 
								 | 
							
								     o utilizar alguna otra funcionalidad del Smarty, esta puede usar el 
							 | 
						|||
| 
								 | 
							
								     objeto <parameter>$smarty</parameter> alimentandolo para hacer eso.
							 | 
						|||
| 
								 | 
							
								    </para>
							 | 
						|||
| 
								 | 
							
								    <para>
							 | 
						|||
| 
								 | 
							
								     Vea tambien:
							 | 
						|||
| 
								 | 
							
								     <link linkend="api.register.function">register_function()</link>,
							 | 
						|||
| 
								 | 
							
								     <link linkend="api.unregister.function">unregister_function()</link>.
							 | 
						|||
| 
								 | 
							
								    </para>
							 | 
						|||
| 
								 | 
							
								    <para>
							 | 
						|||
| 
								 | 
							
								     <example>
							 | 
						|||
| 
								 | 
							
								      <title>Funci<EFBFBD>n de plugin con salida</title>
							 | 
						|||
| 
								 | 
							
								      <programlisting role="php">
							 | 
						|||
| 
								 | 
							
								<![CDATA[
							 | 
						|||
| 
								 | 
							
								<?php
							 | 
						|||
| 
								 | 
							
								/*
							 | 
						|||
| 
								 | 
							
								 * Smarty plugin
							 | 
						|||
| 
								 | 
							
								 * -------------------------------------------------------------
							 | 
						|||
| 
								 | 
							
								 * File:     function.eightball.php
							 | 
						|||
| 
								 | 
							
								 * Type:     function
							 | 
						|||
| 
								 | 
							
								 * Name:     eightball
							 | 
						|||
| 
								 | 
							
								 * Purpose:  outputs a random magic answer
							 | 
						|||
| 
								 | 
							
								 * -------------------------------------------------------------
							 | 
						|||
| 
								 | 
							
								 */
							 | 
						|||
| 
								 | 
							
								function smarty_function_eightball($params, &$smarty)
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								    $answers = array('Yes',
							 | 
						|||
| 
								 | 
							
								                     'No',
							 | 
						|||
| 
								 | 
							
								                     'No way',
							 | 
						|||
| 
								 | 
							
								                     'Outlook not so good',
							 | 
						|||
| 
								 | 
							
								                     'Ask again soon',
							 | 
						|||
| 
								 | 
							
								                     'Maybe in your reality');
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    $result = array_rand($answers);
							 | 
						|||
| 
								 | 
							
								    return $answers[$result];
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								?>
							 | 
						|||
| 
								 | 
							
								]]>
							 | 
						|||
| 
								 | 
							
								</programlisting>
							 | 
						|||
| 
								 | 
							
								     </example>
							 | 
						|||
| 
								 | 
							
								    </para>
							 | 
						|||
| 
								 | 
							
								    <para>
							 | 
						|||
| 
								 | 
							
								     que puede ser usada en el template de la siguiente forma: 
							 | 
						|||
| 
								 | 
							
								    </para>
							 | 
						|||
| 
								 | 
							
								    <programlisting>
							 | 
						|||
| 
								 | 
							
								Question: Will we ever have time travel?
							 | 
						|||
| 
								 | 
							
								Answer: {eightball}.
							 | 
						|||
| 
								 | 
							
								    </programlisting>
							 | 
						|||
| 
								 | 
							
								    <para>
							 | 
						|||
| 
								 | 
							
								     <example>
							 | 
						|||
| 
								 | 
							
								      <title>Funci<EFBFBD>n de plugin sin salida</title>
							 | 
						|||
| 
								 | 
							
								      <programlisting role="php">
							 | 
						|||
| 
								 | 
							
								<![CDATA[
							 | 
						|||
| 
								 | 
							
								<?php
							 | 
						|||
| 
								 | 
							
								/*
							 | 
						|||
| 
								 | 
							
								 * Smarty plugin
							 | 
						|||
| 
								 | 
							
								 * -------------------------------------------------------------
							 | 
						|||
| 
								 | 
							
								 * File:     function.assign.php
							 | 
						|||
| 
								 | 
							
								 * Type:     function
							 | 
						|||
| 
								 | 
							
								 * Name:     assign
							 | 
						|||
| 
								 | 
							
								 * Purpose:  assign a value to a template variable
							 | 
						|||
| 
								 | 
							
								 * -------------------------------------------------------------
							 | 
						|||
| 
								 | 
							
								 */
							 | 
						|||
| 
								 | 
							
								function smarty_function_assign($params, &$smarty)
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								    if (empty($params['var'])) {
							 | 
						|||
| 
								 | 
							
								        $smarty->trigger_error("assign: missing 'var' parameter");
							 | 
						|||
| 
								 | 
							
								        return;
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    if (!in_array('value', array_keys($params))) {
							 | 
						|||
| 
								 | 
							
								        $smarty->trigger_error("assign: missing 'value' parameter");
							 | 
						|||
| 
								 | 
							
								        return;
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    $smarty->assign($params['var'], $params['value']);
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								?>
							 | 
						|||
| 
								 | 
							
								]]>
							 | 
						|||
| 
								 | 
							
								      </programlisting>
							 | 
						|||
| 
								 | 
							
								     </example>
							 | 
						|||
| 
								 | 
							
								    </para>
							 | 
						|||
| 
								 | 
							
								  </sect1>
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								<!-- Keep this comment at the end of the file
							 | 
						|||
| 
								 | 
							
								Local variables:
							 | 
						|||
| 
								 | 
							
								mode: sgml
							 | 
						|||
| 
								 | 
							
								sgml-omittag:t
							 | 
						|||
| 
								 | 
							
								sgml-shorttag:t
							 | 
						|||
| 
								 | 
							
								sgml-minimize-attributes:nil
							 | 
						|||
| 
								 | 
							
								sgml-always-quote-attributes:t
							 | 
						|||
| 
								 | 
							
								sgml-indent-step:1
							 | 
						|||
| 
								 | 
							
								sgml-indent-data:t
							 | 
						|||
| 
								 | 
							
								indent-tabs-mode:nil
							 | 
						|||
| 
								 | 
							
								sgml-parent-document:nil
							 | 
						|||
| 
								 | 
							
								sgml-default-dtd-file:"../../../../manual.ced"
							 | 
						|||
| 
								 | 
							
								sgml-exposed-tags:nil
							 | 
						|||
| 
								 | 
							
								sgml-local-catalogs:nil
							 | 
						|||
| 
								 | 
							
								sgml-local-ecat-files:nil
							 | 
						|||
| 
								 | 
							
								End:
							 | 
						|||
| 
								 | 
							
								vim600: syn=xml fen fdm=syntax fdl=2 si
							 | 
						|||
| 
								 | 
							
								vim: et tw=78 syn=sgml
							 | 
						|||
| 
								 | 
							
								vi: ts=1 sw=1
							 | 
						|||
| 
								 | 
							
								-->
							 |