Magento: how do I access custom variables in PHP? - magento

I am aware of 'Custom Variables' and how they can be used with {{ }} brackets in email templates as well as in static blocks.
However, I want to use them in template code i.e. view.phtml.
I want to be able to access 'variable plain value' to retrieve a conversion value, i.e. a number/string as number for a given 'variable code'.

Been doing this for some time to create various messages that are editable through the admin interface so I don't have to go code digging when the flavor of the moment changes.
To access the plain value of the custom variable with code custom_variable_code use this:
Mage::getModel('core/variable')->loadByCode('custom_variable_code')->getValue('plain');
NOTE: Single store doesn't show the store select dropdown for the variable scope. This answer is not technically correct, in order to future-proof yourself in case of having multiple stores --> Please see #Mark van der Sanden answer below and give him an upvote.

Unfortunately, all other answers are not 100% correct. Use it like this (note the setStoreId() to get the value for the correct store view):
$value = Mage::getModel('core/variable')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCode('variable_code')
->getValue('text');
Or to get the html value:
$value = Mage::getModel('core/variable')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCode('variable_code')
->getValue('html');
If no html value is defined, getValue() returns the text value if you request the html value.

Stackoverflow almost to the rescue again. Thought this would be it:
Setting a global variable in Magento, the GUI way?
But it wasn't, this was:
$angle = Mage::getModel('core/variable')->loadByCode('angle')->getData('store_plain_value');

The only way I see you can acheive this by having a method in the templates block, that will output the needed result.
For instance say in the template view.phtml you have the following code:
<div id="title_container">
<h2><?= $this->getTitle(); ?></h2>
</div>
The function can represent your variable code and any logic that has to do with what gets displayed in the title should be placed in the block.
Just for clarification sake the block is the variable $this
If you are unsure what is the actual class name of your block you can do something like:
Mage::log(get_class($this));
in the var/log/system.log you will print the class of the block of that template.
That is the best way.
HTH :)

// To get the TEXT value of the custom variable:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('text');
// To get the HTML value of the custom variable:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('html');
// The store id is set as Custom Variables can be edited for multiple stores

Note: A custom variable might have different values for different stores.
So to access store specific value for the custom variable with the code custom_variable_code
Use this:
$storeId = Mage::app()->getStore()->getId();
$custom_variable_text = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('text');
$custom_variable_plain_value = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('plain');
$custom_variable_html_value = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('html');

Related

Is it possible to manipulate the output of a laravel #yield in the blade template?

Wondering if it is possible to manipulate the output of a #yield in Laravel? In short I have a title in my child template that I would like to set some id's with to make them unique. I know I could just create another #section('id', 'asdf') but rather not have to worry users with that especially since the title has to be unique anyways...
Could not find anything that says this is possible?
Example (which fails) but what I am essentially trying to do:
id="{{Str::kebab(#yield('title'))}}-preview-tab"
#yield gets replaced with a PHP echo statement so that is not what you want. If you want the content of a section you can grab it from the View Factory:
$__env->getSection($name, $default)
Or even calling yieldContent:
$__env->yieldContent($section, $default)
So you could try:
{{ Str::kebab($__env->getSection('title', 'some default if you want')) }}
If you have any issues with that, try the yieldContent method.

assign value from attribute_view_gui from select attribute

I have an attribute if type select. When i try to get value from this attribute content it gives the identification number instead of the value. I call like
$node.data_map.my_attribute_identifier.content
This is expected behaviour. https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Datatypes/Selection
Raw output
The ".content" of an ezcontentobjectattribute object using this datatype returns an array of the identification numbers (as strings) of the selected options.
I want the value not the identification number. I can get that using attribute_view_gui like
attribute_view_gui attribute=$node.data_map.my_attribute_identifier
But i can't assign value to a variable this way. How can i assign value from a select attribute?
First of all i recommend you to always check default templates in your ezpublish to figure out how should template look...
Maybe this example will help:
<input
id="whatever_id_you_like"
type="text" size="50"
name="ContentObjectAttribute_ezstring_data_text_{$node.object.data_map.YOUR_ATTRIBUTE_SHORT_NAME.id}"
value="{$YOUR_VAR}"
/>
or u can use default view for attribute like this:
{attribute_view_gui attribute=$node.data_map.YOUR_ATTRIBUTE_SHORT_NAME}
also might be helpful - way to find correct path (sometimes you need add ".data_int" or ".data_text" on the end of the path to display data):
{$path|attribute(show,depth)} example:
{$node|attribute(show,2)}
or
{$YOUR_FANCY_VAR.content|attribute(show,2)}
You may want to take a look at the view template of ezselection:
ezselection.tpl
This is the code that eZ Publish uses to view the data type.
content of ezselection.tpl:
{let selected_id_array=$attribute.content}
{section var=Options loop=$attribute.class_content.options}
{section-exclude match=$selected_id_array|contains( $Options.item.id )|not}
{$Options.item.name|wash( xhtml )}{delimiter}<br/>{/delimiter}{/section}
{/let}

Variable Variable in Smarty Templates

I have a dynamically generated Smarty variable in PHP. I want to access it with name ,
Say for example there is a smarty variable {$asdf} which was generated dynamically and i have an array that has 'asdf' i want to use this array and access {$asdf}.
{$asdf} prints a input element [rendered] ;
$array = array('asdf');
{foreach from=$array item=x}
{$x}
{/foreach}
//but {$x} is not giving renderend input instead it is giving $asdf
where am i going wrong?
It is generally atypical to do this type of work in a template file. You should separate your template and logic as much as possible -- there's no plausible scenario in which you could not simply prepare the needed variables for your template in php and pass them on to the template in a useable structure.
That said, it is possible. Within a template, all variables that were passed to the template are accessible in an array, Smarty::_tpl_vars. Within a template, one may interact with this array using the {php}{/php} tags, where it can be referenced via $this --
{php}
$unknownValue = $this->_tpl_vars[
$this->_tpl_vars['known_key']
];
// for example...
$this->_tpl_vars['magicalValue'] = $unknownValue;
{/php}
Magic: {$magicalValue}
I cannot reiterate enough, however, that it is generally bad practice to place such logic inside a template.

How popular article module is working?

/modules/mod_articles_popular/tmpl/default.php
Can somebody explain me, how this working?
I do not understand how these $item->link and $item->title get correct information?
Where is MySQL query? Is it global variables? If yes, where they are described?
Any advice is much appreciated.
So, like most modules /tmpl/default.php is included on the last line the module entry point file i.e. mod_articles_popular.php
In that file helper.php is included first followed by
$list = modArticlesPopularHelper::getList($params);
As you can see this calls the getList() method of the helper class which performs the task of retrieving the $list of articles.
It (modArticlesPopularHelper) in turn loads the ContentModel and sets the state of the $model based on the default app params and the modules settings.
The it asks the model for the actual items required with the line $items = $model->getItems().
After that it loops through the items returned by the model and creates a link value for each article prior to returning it to the module.
As a result $list is filled with each of the article items which are pulled out individually in the foreach loop in /tmpl/default.php file.

Page-specific logic in Joomla

I am trying to enable JavaScript in a Joomla template to behave differently depending on the page. In particular, I have set the Key Reference as that appears to be the most appropriate value I could find for this purpose. Unfortunately, I can't seem to access it in my code. I tried:
$this->params->get("keyref")
and a few other variations, but they simply returned a blank. How can I retrieve this value or is there a better way of writing page specific logic.
Related Articles
Joomla load script in a specific page: This would work, but seems like overkill for what I want to do here.
Each page can be given an alias. We can retrieve the alias using code from the forum:
function getCurrentAlias()
{
$menu = &JSite::getMenu();
$active = $menu->getActive();
return $active->alias;
}
We can then inject this into the Javascript:
var alias= '<?php echo getCurrentAlias(); ?>';
I'm not aware of keyref, but I would solve it by using the class suffix parameter you can set for each menu entry.see I would use a space in front of this suffix. With javascript you can then try to read this classname (suffix without the space) on each page.
getElementsByClassName("mysuffix");
for example
If this returns multiple objects, you know on which page you are. Will that help you?

Resources