Template variable used in content

About the template and stylesheet - and changing the menu
Post Reply
Tata
Posts: 3586
Joined: Tue May 20, 2008 5:34 am
Location: Slovakia
Contact:

Template variable used in content

Post by Tata » Mon Feb 13, 2017 7:48 am

I need to use

Code: Select all

$plugin_cf['value']['value']="value"; 
in the content. How can I make it possible?
Or better, I want to let guests to test a plugin without logging in. For this a values in a language file shall be changed to see the effect.
E.g.
The plugin used o this page shall publish three various pages in a news box:
1. before a "start-time"
2. during some specified time tange
3. after the "stop-time"
These values are set in a language file in:

Code: Select all

$plugin_cf['name']['start_time']="06:00"; 
$plugin_cf['name']['stop_time']="60 minutes" 
Last edited by Tata on Mon Feb 27, 2017 12:13 am, edited 3 times in total.
CMSimple.sk
It's no shame to ask for an answer if all efforts failed.
But it's awful to ask without any effort to find the answer yourself.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Template bariable used in content

Post by cmb » Mon Feb 13, 2017 9:08 am

Tata wrote:I need to use

Code: Select all

$plugin_cf['value']['value']="value";
in the content. How can I make it possible?
Basically, with CMSimple-Scripting:

Code: Select all

#CMSimple $plugin_cf['value']['value']="value";#
However, if one or more of the values have to be user input, the user has to supply these values somehow. For instance, the values might be passed as URL parameter: http://example.com/?&value1=foo&value2=bar&value3=baz (you can prepare respective links). To use these parameters you could do:

Code: Select all

#CMSimple $plugin_cf[$_GET['value1']][$_GET['value2']]=$_GET['value3'];#
However, if you accept user input you really should validate and/or sanitize the input, so it makes sense to write a dedicated function, and to call this from the content.

Also note that a request that might change the server status (for instance, the contents of file on the server) should be a POST request, so it's best to offer a small form to the user.
Christoph M. Becker – Plugins for CMSimple_XH

Tata
Posts: 3586
Joined: Tue May 20, 2008 5:34 am
Location: Slovakia
Contact:

Re: Template bariable used in content

Post by Tata » Mon Feb 27, 2017 12:05 am

Another scenario:
I need to use the same string (text and/or numbers) on more places within a webpage (e.g. some title, price, time etc.).
The strings/values may be defined in configuration or in language files. I hoped it would work with

Code: Select all

{{{$plugin_tx['plugin']['string'];}}} 
.
But it doesn't. It simply retuns just the
{{{$plugin_tx['plugin']['string'];}}}
. What is the trick?
Longer texts ate simple manageable by Boilerplate. But for short texts (esp. times, prices etc.) it seems to be to demanding to use a plugin for it.
CMSimple.sk
It's no shame to ask for an answer if all efforts failed.
But it's awful to ask without any effort to find the answer yourself.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Template bariable used in content

Post by cmb » Mon Feb 27, 2017 12:59 am

Tata wrote:Another scenario:
I need to use the same string (text and/or numbers) on more places within a webpage (e.g. some title, price, time etc.).
The strings/values may be defined in configuration or in language files. I hoped it would work with

Code: Select all

{{{$plugin_tx['plugin']['string'];}}}
.
But it doesn't. It simply retuns just the
{{{$plugin_tx['plugin']['string'];}}}
. What is the trick?
The plugin call syntax requires a function call. If you just want to output the value of a variable you could do something like:

Code: Select all

#CMSimple $output .= $plugin_tx['plugin']['string'];#
That places the output at the end of the page, though. If you want to put the output where the plugin call is placed, you could use a little trick instead:

Code: Select all

{{{trim($plugin_tx['plugin']['string'])}}}
And of course, you could abstract that away by defining an appropriate function (for instance, in index.php):

Code: Select all

function my_plugin_tx($key)
{
    global $plugin_tx;

    return $plugin_tx['plugin'][$key];
}
and call that on a page:

Code: Select all

{{{my_plugin_tx('string')}}}
Christoph M. Becker – Plugins for CMSimple_XH

Tata
Posts: 3586
Joined: Tue May 20, 2008 5:34 am
Location: Slovakia
Contact:

Re: Template variable used in content

Post by Tata » Mon Feb 27, 2017 8:40 am

Excellent!
This way it really works both

Code: Select all

{{{trim($plugin_tx['plugin']['string'])}}} 
and

Code: Select all

{{{my_plugin_tx('string')}}} 
but the function is very specific for the very unique plugin.
I tried to make it more versatile to be used as an AddOn this way

Code: Select all

// Read & Insert value
function value_cf($plugin,$key)
{
    global $plugin_cf, $plugin, $pth, $sl;
    $value_cf = $plugin_cf . ',' .$key;
    return $value_cf;
}
function value_tx($key)
{
    global $plugin_tx, $plugin, $pth, $sl;
    $value_tx = $plugin_tx . ',' . $key;
    return $value_tx;
} 
So that the AddOn would read all plugins, their config and/or language variables and return them to respective functions. It works (doesn't return any errors, at least), but doesn't return the values. It returns only something like
Array,event_duration
I speak 5 foreign languages. But my grammar in this one is evidently too weak.
CMSimple.sk
It's no shame to ask for an answer if all efforts failed.
But it's awful to ask without any effort to find the answer yourself.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Template variable used in content

Post by cmb » Mon Feb 27, 2017 10:04 am

Tata wrote:I tried to make it more versatile to be used as an AddOn this way
[…]
Try:

Code: Select all

// Read value
function value_cf($plugin,$key)
{
    global $plugin_cf;

    return  $plugin_cf[$plugin][$key];
}
function value_tx($plugin, $key)
{
    global $plugin_tx;

    return  $plugin_tx[$plugin][$key];
}
Christoph M. Becker – Plugins for CMSimple_XH

Tata
Posts: 3586
Joined: Tue May 20, 2008 5:34 am
Location: Slovakia
Contact:

Re: Template variable used in content

Post by Tata » Mon Feb 27, 2017 10:58 am

+1
CMSimple.sk
It's no shame to ask for an answer if all efforts failed.
But it's awful to ask without any effort to find the answer yourself.

Post Reply