solved Smarty Function for HTML

SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Hi - We have a block of HTML that we may need to paste in several locations, maybe across JR sites.
--
We prefer isolate the code into one reusable function, instead of having the same code copied over and over. In WordPress we would simply create a quick plugin. How to achieve this in JR?
--
Is smarty function the correct way? If so, any docs to explain how to get started, and specifically where exactly does the code go? Is this done thru ACP or must use FTP?
--
Can we "wrap" the HTML in a Smarty Function, and just paste short code in the site markup?
updated by @softdesigns: 01/13/18 01:24:20PM
paul
@paul
7 years ago
4,332 posts
Yes - Use a custom smarty function. Create it with a custom module with just an include.php file that looks like this -
<?php

// make sure we are not being called directly
defined('APP_DIR') or exit();

/**
 * meta
 */
function xxHTMLSmarty_meta(){
    $_tmp = array(
        'name'        => 'HTMLSmarty',
        'url'         => 'htmlsmarty',
        'version'     => '1.0.0',
        'developer'   => 'SoftDesigns &copy;' . strftime('%Y'),
        'description' => '????',
        'license'     => 'mpl',
        'category'    => 'admin'
    );
    return $_tmp;
}

/**
 * init
 */
function xxHTMLSmarty_init(){
    return true;
}

/**
 * Smarty function to get html string
 * @param $param string Param name
 * @return string
 */
function smarty_function_xxHTMLSmarty_get_html($param)
{ return 'The html string to be returned goes here!!'; }

Then in the templates just use {$xxHTMLSmarty_get_html}

If you have several html strings you might want to return, pass in a parameter and return on that -

<?php

// make sure we are not being called directly
defined('APP_DIR') or exit();

/**
 * meta
 */
function xxHTMLSmarty_meta(){
    $_tmp = array(
        'name'        => 'HTMLSmarty',
        'url'         => 'htmlsmarty',
        'version'     => '1.0.0',
        'developer'   => 'SoftDesigns &copy;' . strftime('%Y'),
        'description' => '????',
        'license'     => 'mpl',
        'category'    => 'admin'
    );
    return $_tmp;
}

/**
 * init
 */
function xxHTMLSmarty_init(){
    return true;
}

/**
 * Smarty function to get an html string
 * @param $param string Param name
 * @return string
 */
function smarty_function_xxHTMLSmarty_get_html($param)
{ $_html = array( 1 => 'HTML string 1', 2 => 'HTML string 2', 3 => 'HTML string 3', ); return $_html[$param['id']; }

which would be called as {xxHTMLSmarty_get_html id=2}

If the possible html strings to be returned need to be more dynamically configurable, create the module using the Aparna module then add a smarty fuction to that to return html strings created by admin and stored on a datastore.
hth


--
Paul Asher - JR Developer and System Import Specialist
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Ok - Cool - mostly this makes sense
--
One Question: It seems like this module is like all our other custom modules? From your code, the key point to make it a smarty function is this code function:
function smarty_function_xxHTMLSmarty_get_html($param)

Since we have created many custom modules before, could we say - this is similar to our other custom modules except :
To add any new smarty functions, create this type of new function:
function smarty_function_xxMyFuncName

Does this sound correct?
paul
@paul
7 years ago
4,332 posts
Yes, you could just add the smarty function to an existing module if need be. Just make sure the function name includes the correct module name.


--
Paul Asher - JR Developer and System Import Specialist
michael
@michael
7 years ago
7,746 posts
A bit old, see if this doc helps at all, if not, let me know what needs adding (probably a bunch :) )

Docs: "Defining your own smarty function"
https://www.jamroom.net/the-jamroom-network/documentation/module-developer-guide/1569/defining-your-own-smarty-function
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Thanks guys - Our first custom Smarty function now seems working :)

Great Support - Solved...
updated by @softdesigns: 10/14/17 06:36:59AM

Tags