how could I add a post var as a param of a template tag in magento? - templates

Inside the template account_new_confirmation, which is the template of the confirmation mail the user receives after he creates a new account, I need to add a param inside the tag
{{store url="customer/account/confirm/" _query_id=$customer.id _query_key=$customer.confirmation _query_back_url=$back_url _query_myparam="TEST"}}
this is working for a string producing the following link to be written inside the confirmation mail as expected:
http://baseurl/customer/account/confirm/?id=12345&key=donkey&back_url=monkey&myparam=TEST
but I cannot figure out how to replace the string TEST with the value of a param I have in request post.
I mean the user reach this point after having filled a form sent with POST action. Inside this form I have a checkbox named FOO and I need to add its value (true or false) to _query_myparam on my example above.
I tried with
_query_param=$foo
and with
_query_param=$this->getRequest()->getPost('foo')
but they both were too easy to work.
Anyone knows how to solve this?

I found it myself :D
You have to change the method sendNewAccountEmail of the class Mage_Customer_Model_Customer which you can find in app/code/core/Mage/Customer/Model/Customer.php
You need to add new variables available inside the template inside:
array('customer' => $this, 'back_url' => $backUrl)
So for your need this would be changed in:
array('customer' => $this, 'back_url' => $backUrl, 'foo' => Mage::app()->getRequest()->getPost('foo'))
Then you can add this variable to the template param as
{{store url="customer/account/confirm/" _query_id=$customer.id _query_key=$customer.confirmation _query_back_url=$back_url _query_myparam=$foo}}
This will produce the following link:
http://baseurl/customer/account/confirm/?id=12345&key=donkey&back_url=monkey&myparam=on
when checkbox FOO is checked.
Thanks

Related

Can you make WooCommerce shortcodes from template parts?

I'm interested in creating shortcodes that basically fill in template parts of the WooCommerce checkout. For example, in functions.php of my child theme:
function shortcode_review_order() {
//get the template part from woocommerce/templates/checkout/review-order.php
wc_get_template_part('checkout/review-order');
}
add_shortcode( 'custom_review_order', 'shortcode_review_order' );
...and then in my page...
<div>[custom_review_order]</div>
When I tried this, nothing appeared in my checkout page.
Is this even possible?
there are few things wrong in your code...
first, you should add a shortcode using the init hook.
add_action( 'init', 'add_shortcodes' );
function add_shortcodes(){
add_shortcode( 'custom_review_order', 'shortcode_review_order' );
}
then you lack the .php part of the template. Also it needs the array parameter like below. And you might get more accurate result using wc_get_template.
function shortcode_review_order(){
wc_get_template( 'checkout/review-order.php', array( 'checkout' => WC()->checkout() ) );
}
To know more on how to correctly use it's template, search for each on the plugin. You'll see how it's being used. And you can get a hint on how you can use it for yourself.
I found that wc_get_template echos out the template, instead, for shortcodes returning the template is better. You can use:
$string = wc_get_template_html( $template_name, $args, $template_path, $default_path );
It is "Like wc_get_template, but returns the HTML instead of outputting."
https://docs.woocommerce.com/wc-apidocs/function-wc_get_template_html.html

Revel: pass template var to url

I want to understand how url helper works.
For example, in my template I have:
my super url
and in controller:
func (c Pages) IndexPages() revel.Result {
...
}
I need url like
http://localhost:9000/pages?page=1
I don't want to write:
func (c Pages) IndexPages(page int) revel.Result {
because I want to check if the controller contains the param page.
How to add my template var to c.Params.Query with the url helper?
Revel url helper code in template.go
// Return a url capable of invoking a given controller method:
// "Application.ShowApp 123" => "/app/123"
func ReverseUrl(args ...interface{}) (template.URL, error) {
We need to update the manual with information about this template function, but you can see in the link and code above how it's intended to be used. You pass it a controller and action with parameters and it creates a template.URL object with the matching route.
It seems you're not interested in how the url helper works (though that's what you asked). You want to know how to pass the page variable to the template? In your controller you need to pass page via c.RenderArgs["page"] = page. Then you can reference it in your template: my super url Revel Template Doc.
As you noted, you can manually get the value for page with page := c.Params.Query.Get("page") if you don't want to use the parameter binding feature.

Grails: pass a List from GSP to controller with remote link

So, in my method A in the controller ServicioComunitario I send this to the GSP:
tg = ServicioComunitario.findAll("from ServicioComunitario as b where "+query)
[servicioComunitarioInstanceList: tg, params: params]
Then in the GSP I call another method (generarDocDeReporte) of ServicioComunitarioController:
<g:set var="b" value="${'xls'}"/>
<g:set var="a" value="${'excel'}"/>
<g:set var="servicioLista" value="${servicioComunitarioInstanceList}"/>
<g:link controller="ServicioComunitario" action="generarDocDeReporte"
params="${[exportFormat:a, extesion:b, tg: servicioLista] }"
update="mainContent">Excel</g:link><br/>
Then, in the new method "generarDocDeReporte" I have:
println params.exportFormat+"-"+params.extesion
if(params.tg)
println "Not empty"
exportFormat and extension work as expected, but the params.tg doesn't seem to behave normal.
I am trying to use this new params.tg where it was a ServicioComunitario.list(params):
exportService.export(params.exportFormat, response.outputStream, ServicioComunitario.list(params), fields, labels, formatters, parameters)
And here is where I get the error:
exportService.export(params.exportFormat, response.outputStream, params.tg, fields, labels, formatters, parameters)
When I receive the params.tg, do I need to cast it? or what do you think is the error?
Thank you very much in advance
You can't just pass a list of instances like that in a link. You can however collect the ids into a list as a parameter and then use it to populate it later. For example:
<g:link controller="ServicioComunitario" action="generarDocDeReporte"
params="${[exportFormat:a, extesion:b, tgids: servicioLista.collect{it.id}.join(',')] }"
update="mainContent">Excel</g:link><br/>
And then in your controller where you need to get the list again:
def tg = ServicioComunitario.getAll(params?.tgids?.tokenize(","))
Also, you don't need to assign params to params when returning your model. parameters are already exposed in the GSP by convention.
[servicioComunitarioInstanceList: tg]

Codeigniter + Dwoo

I got problem when implementing my CMS using Codeigniter 1.7.2 and Dwoo. I use Phil Sturgeon Dwoo library. My problem is I want user create template from the admin panel, it means all template will be stored into database including all Dwoo variable and functions.My questions:
Is it possible to load dwoo template from database?
How to parse dwoo variable or function from database? I tried to load content from database which is include dwoo var and function inside it, and i have tried to do evaluation using dwoo eval() function and phil sturgeon string_parse() but still have no luck.
for example:
my controller
$data['header'] = "<h1>{$header}</h1>"; --> this could be loaded from database
$this->parser->parse('header',$data);
my view
{$header}
This is the error message:
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined index: header_title</p>
<p>Filename: compiled/805659ab5e619e094cac7deb9c8cbfb5.d17.php</p>
<p>Line Number: 11</p>
header_title is dwoo variable that loaded from DB.
Thank you,
It is definitely possible to do this, but for performance reasons it would probably be faster to store the templates as files on the filesystem, even if they're edited by users. If you're smart with file naming you can avoid most hits to the database.
Anyway, if you really want to do it with the database, the following should work:
// rendering the header
$template = '<h1>{$header}</h1>'; // loaded from db, don't use double-quotes for examples or $header will be replaced by nothing
$data = array('header' => 'Hello'); // loaded from db as well I assume
$headerOutput = $this->parser->parse_string($template, $data, true); // true makes it return the output
// now rendering the full page (if needed?)
$data = array('header' => $headerOutput);
$this->parser->parse('header', $data); // no 'true', so goes straight to output
The view would then contain {$header} and the output from the header template is passed to that variable.
Now I'm not sure how CI works so it might be better to just output the result of the first template and skip the second one entirely, but I'll leave that up to you.

Drupal preprocess function and session

I set a session var at template preprocess function in a theme I use, but the first time I open the site I cant read the session var, if I refresh the page it works fine, anybody know what can be the problem??
This is the code I use in the preprocess function:
function m_preprocess(&$vars, $hook) {
$default_location = array(
'country_code' => 'ec',
'province_code' => 'p',
'province' => 'Pichincha',
'city' => 'Quito',
'city_path' => 'lugares/u/ec/p/*'
);
if (isset($_COOKIE['proximity_path'])) $default_location['proximity_path'] = $_COOKIE['proximity_path'];
$default_location['path'] = isset($_COOKIE['sort-by']) && $_COOKIE['sort-by']=='proximity'? $_COOKIE['proximity_path'] : $default_location['city_path'];
$_SESSION['location'] = $default_location;
}
A couple of things:
Try dsm($_SESSION); to see what is the var content when the site first load.
I don't know where you create $_COOKIE['proximity_path'], but it is not in the code you show.
Check for typos
The template pre-process function is called before node.tpl.php (that's why it is called pre-process) and that is why the value of the $_SESSION variable is not available in the template preprocess function.
Like you pointed out, it works after the first page load. This happens when only after $_SESSION variable is set in the node body (using the PHP filter)