How to pass a new variable to template.xml file - xenforo

I have installed my own custom theme to xenforo. In my template.xml file i can see the usage of some variable like {$requestPaths.fullBasePath} in the head section. If i want use another variable like these in the header section, where should i defined this variable and from where we can assign values o that variable?

You can do that in the controller, something similar to this:
$viewParams = array(
'variableName' => $variableValue,
'variableName2' => $someOtherValue,
'someArray' => array(
'foo' => 'bar'
)
);
return $this->responseView('MyAddOn_ViewPublic_SomeViewClass', 'some_template', $viewParams);
Then in your template, you can use those variables with the curly syntax:
{$variableName} // output $variableValue with html escaped
{xen:raw $variableName2} // output $someOtherValue
{$someArray.foo} // output "bar"
There are other ways to pass variables to template too: using template_create event listener or <xen:container /> but that's quite complicated. For more information regarding add-on development, read here.

Related

Yii2: Translate Application Name

How to properly translate the name of the application in Yii2?
We can easily set the application name in main-local.php (or config/main.php) like so:
$config = [
'name' => 'My Application Name',
// ...
];
But how would we translate it?
Using something like \Yii::t('app.name', 'My Application Name') does not work because the configuration file is parsed before the application language is even determined or set.
The easiest way is to do translation on actual usage of application name:
<?= \Yii::t('app.name', Yii::$app->name) ?>
For messages extraction you may use fake translation in comment. Not sure about Poedit, but built-in Yii extractor support this some time ago:
$config = [
// \Yii::t('app.name', 'My Application Name')
'name' => 'My Application Name',
// ...
];
In worst case you may create separate file for such fake translations only for messages extraction, and don't include it on actual execution.

Creating URL and params in Yii2

I'm trying to create Url with multiple param using Url::to() method but it's not working i also tried UrlManager->creatUrl() method but no luck, i have pretty Url enable and if i use only one query param it works fine but when i try to add more i got error, below is my code.
$linkUrl = \Yii::$app->UrlManager->createUrl(['sell/updatecategory', ['draftId'=> $model->draftId,'catid' =>$model->category_id]]);
<?= Html::button('Change category or product',['value'=>$linkUrl, 'id' => 'StartOver']) ?>
another try is:
$linkUrl = Url::to(['sell/updatecategory', ['draftId'=> $model->draftId,'catid' =>$model->category_id]]);
in the two case above the url output always look like this:
GET http://http://192.168.199.128/trobay/products/sell/updatecategory?1%5BdraftId%5D=20&1%5Bcatid%5D=50
and the server throw an error cannot resolve the url, what i want is something like this:
GET http://http://192.168.199.128/trobay/products/sell/updatecategory?draftId=20&catid=50
The system added some character which i guess is the cause of the problem but don't really know how to remove this. i hot anyone could help with this thanks
Don't use nested array for parameters.
Structure should look like this:
[0 => route, param1 => value1, param2 => value2, ...]
so in your case
$linkUrl = Url::to([
'sell/updatecategory',
'draftId' => $model->draftId,
'catid' => $model->category_id
]);

How can I remove quotes in rss

I have this in my RSS feed, in drupal views:
<link>Prueba "con comillas"</link>
I've tried to create a module like this:
function views_without_encoded_preprocess_views_view_views_rss(&$vars) {
if (!empty($vars['rss_feed'])) {
$vars['rss_feed'] = strtr($vars['rss_feed'], array(
'&#039;' => ''',
'&quot;' => '"',
'&lt;' => '<',
'&gt;' => '>',
'&amp;' => '&',
'"gt' => '',
));
}
}
but everything is not ok. I continue seeing this part:
<link>Prueba "con comillas"</link>
Only for quotes.
As I see you try to use the Views RSS.
There is a fix that appears to work but it has been tested only at Drupal 6 sites. In Drupal 7 some things changed but try out this one:
Go to views_rss/theme and open the theme.inc
Copy out the entire 'function template_preprocess_views_view_views_rss function, and put it in your theme's template.php.
Change the function name to: function yourthemename_precrocess_views_view_views_rss
Then at line 200 in the original theme, or where it reads '// Add XML element(s) to the item array' insert the following just above:
if (empty($rss_elements)) continue;
// Insert here -- clean up special characters
$rss_elements[0]['value'] = htmlspecialchars_decode(trim(strip_tags(decode_entities( $rss_elements[0]['value'])),"\n\t\r\v\0\x0B\xC2\xA0 "));
$rss_elements[0]['value'] = htmlspecialchars($rss_elements[0]['value'], ENT_COMPAT);
// end of cleaning
// Add XML element(s) to the item array.
$rss_item['value'] = array_merge($rss_item['value'], $rss_elements);
}
Check your RSS.... you might have to flush the cache a few times.
Another thing you could try is htmlspecialchars. It seems to me that the output of the Views RSS fields could use this to force encoding on quotes, apostrophes, and ampersands.
Hope that helps.

Use a widget in a statically-called method

Normally a widget is used by calling CController::widget() on an instance of CController, typically $this in a view.
But if I'm writing a static method, a helper, say, then I don't have access to an instance of CController. So how do I use a widget?
Let's say further that this helper method is invoked in the eval()’ed expression in a CDataColumn's value property. That poor expression has almost no context at all. How should the helper use a widget?
EDIT: Code example
As requested, a view example:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'columns' => array(
array(
'name' => 'attrName',
'value' => '--USE WIDGET HERE--',
),
)
));
This answer doesn't answer the question in general but in the specific case—how to access the controller and use a widget in the context of the evaluated expression of CDataColumn::$value—you can use this:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'columns' => array(
array(
'name' => 'attrName',
'value' => function ($data, $row, $column) {
$controller = $column->grid->owner;
$controller->widget(/* ... etc ... */);
},
),
)
));
The trick was discovering that CDataColumn::renderDataCellContent() uses CComponent::evaluateExpression(), which injects the component instance into the callback as the last parameter. In this case that omponent is the CDataColumn, which references the controller as shown.
I don't like writing PHP expressions as string literals so I'm pleased to find this option.
A comment on http://www.yiiframework.com/doc/api/1.1/CDataColumn#value-detail shows another way to us a widget in a column value that I haven't tried.
This one is working solution for calling widgets in static methods in Yii
Yii::app()->controller->widget('widget');
There's no direct way to call a widget out of controller because you shouldn't do so. It's all about MVC. Widgets are only needed and/or useful in views, and views are only accessed via controllers. That's the theory.
I guess you're approaching the problem mistakenly. A proper, MVC-friendly way to do what your're trying to do involves using renderPartial(). You know: you a have certain content and you want to decorate it (in your case you want to imbibe it inside a widget, right?) before displaying it to final user; so, from the view, you call renderPartial(). It will send your data to a file where it will properly decorated. renderPartial() returns the content properly formatted and now you can display it in the view.
Unfortunately, in your particular case, you're working with grid view (right?) and, at least from my point of view, it makes the things a bit harder. In order to decorate content for a CGridColumn-subclass element (like CDataColumn), you need to override the renderDataCellContent() method. Check it out here: http://www.yiiframework.com/doc/api/1.1/CDataColumn#renderDataCellContent-detail

Zend Forms and Ext.grid.Panel

I am working for a company who use tabulated html/JS interfaces. These are home grown (real honest to god s) with query events attached to each cell. For the old usage they were suitable, but the interactions required between rows and cells are becoming much more complex on the client side. Specifically they want both server and client side validation.
To facilitate this, the devs I report to are super keen on Zend_Forms, and insist that to use a framework like ExtJS, they don't want to have to write back end and front end code twice (please ignore that if it's all home grown they'll have to do this anyway).
So with that in mind, I'm trying to leverage Zend_Form decorators to create Ext.grid.Panel column defintions. For this, I would need to use decorators to export an array (and then json it using the ViewHelper), or render a JSON string directly.
So this would be something like:
$dateElement = new Zend_Form_Element_Text('startDate', array(
'label' => 'Start Date',
'validators' => array(
new Zend_Validate_Date()
)
));
echo (string)$dateElement;
would output:
{ text: 'Start Date', dataIndex:'startDate', xtype:'datecolumn'}
or (obviously not with string cast, but maybe with ->toArray() or something):
array( 'text' => 'Start Date', 'dataIndex' => 'startDate', 'xtype' => 'datecolumn')
I think if I could get it to this stage, I could get what I need out of it.
Has anyone here tried to do anything similiar to this (getting a JSON/XML/other markups output, rather than HTML from Zend_Forms using Decorators) or if they could point me to any resources?
I think I have a solution...
Make a decorator similar to this:
class My_Form_JSON_Decorator extends Zend_Form_Decorator_Abstract{
protected $xtype;
protected $dataIndex;
public function __construct($dataIndex,$xtype){
$this->xtype=$xtype;
$this->dataIndex=$dataIndex;
}
public function render($content){
$element=$this->getElement();
$label=$element->getLabel
//if you need errors here too do the same with $element->getMessages();
return 'array ("text"=>"'.$label.'","dataIndex"=>"'.$this->dataIndex.'","datecolumn"=>"'.$this->xtype.'")';
}
}
Then, on the form, use something similar to this:
$dateElement = new Zend_Form_Element_Text('startDate', array(
'label' => 'Start Date',
'validators' => array(
new Zend_Validate_Date()
)
$dateElement->setDecorators(array(
new My_Form_JSON_Decorator("startDate","datecolumn");
));
And finally, on the View, you should have this:
{
Date: <?php echo $this->form->startDate; ?>,
}
I didn't tried the code above but, I did it with a similar code I used once when I needed to change Decorators of a Form.
It could not be all correct but, I think that it shows you a way of doing that.
Good work =)