21 Oct Ways to pass variables into a $S script
I try to stay active in the comments sections on Saxotech’s wiki. Whenever I learn something new or find something in the docs that didn’t work exactly right, I post it in the comments. I appreciate when other developers in the Saxotech/Newscycle community do this, so I try to keep an active conversation going there. I also add myself as a follower/watcher on pages I use often so I get an email notification when someone else adds a comment. Which leads me to the point of this week’s blog post.
Has anyone found a way to pass another variable to $S (Runs a script, which name is given as the input parameter.) besides the tag contents?
Sarah Stapleton-Gray with the Bay Area News Group posted a question on the object script docs page asking how to pass variables into a $S script. This is a great question that doesn’t have clear documentation. This blog post is an answer to that question.
I’ll describe 3 possible ways to pass variables to a $S script:
- Set custom variables inside object file calling the $S script
- Static localdata.ini variables
- Use Custom Tag Paramaters (only cce and newslist)
1. Set custom variables inside object file calling the $S script
A $S script can access all variables inside the object file that contains the $S call. Simply create a custom variable inside the object file, then reference that variable in the script. This is the solution that worked for Sarah.
I created some sample files to illustrate how this works.
Create a template calling your pbs tag and the subsequent object files. I used a newslist listing our blogs in this example.
Inside the newsItem99.pbo object file, we set two custom variables:
And then we call the script on the <%counter%> variable. It doesn’t quite matter in this case which variable we use to call the script.
Inside the script we can access the custom variables we created in the object file by using the SOSE GetVar method.
You can see this exact code running here: http://www.tamberra.com/section/scripttest
2. Static localdata.ini variables
You can set static variables inside localdata.ini and reference them in your script using the same SOSE GetVar method as referenced above.
Simply create a static variable inside localdata.ini.
And then reference it in your script file.
3. Use Custom Tag Paramaters (only cce and newslist)
The newslist and cce tags allow you to reference tag paramaters in the object files by using the variable <%tagparamXXXX%> where XXXX is the name of the parameter. I use this feature to my advantage to pass in values I need to use in the object files. This would also work for the $S script.
I pass in two parameters that are custom for me, substrlen and stubstrlen2:
Using similar object files as the first solution above, except we reference the custom tag parameters we passed into the pbs tag:
And then in the script, we get the variables using the SOSE GetVar method:
Have you passed variables into a $S called script in an interesting way? Share with us how you did it in the comments below. Any questions about what we did here? We’d like to hear from you!
Latest posts by Stacey Jenkins (see all)
- Display your youtube playlists or channels on your site directly from Youtube - June 7, 2014
- You say you want PHP SOSE examples? - January 14, 2014
- Building a better UX on CCE submit forms - December 5, 2013