Hi,
I have been writing a small snippet from which i need to return a variable (a URL) that will be used in 3 or 4 places within 1 template.
My question is what is the best way to print the returned URL to multiple places - I’m assuiming that by placing the snippet name ([[redirect]]) in multiple places it runs the code multiple times also. So, is there a way of creating a variable that can be reused?
for example:
Snippet name:
[[redirect]]
Cut down php snippet code:
$redirect = "somedomain.com/particular_folder";
I have tried the following methods to print out the variable to the template:
Create another snippet with the php code (’echo $redirect’)
[[404-campaign-redirect]]
Inline php directly within the template
<?php echo $redirect ?>
Placeholder
+redirect+
Thanks in advance for any help on this.
Create your snippet which will create the placeholder, for this example I named it "my_redirect".
Call it in the top of your template: [[my_redirect]] or [!my_redirect!]
Now you should be able to use the placeholder [+my_redirect+] anywhere you want the output to appear.
<?php
// my_redirect
// create placeholder [+my_redirect+]
$modx->setPlaceholder('my_redirect','somedomain.com/particular_folder');
?>
try in your snippet:
<?php
$redirect = "somedomain.com/particular_folder";
$modx->setPlaceholder('myredirect',$redirect);
in your template where you need it:
[Edit]
Breezer was faster with the same suggestion.