Simple python page template
How to create a simple page template that calls a python script and displays the results in plone.
- In the ZMI navigate to
- Portal Skins -> Custom
- Now using the right hand dropdown add a 'Page Template'
Give it the ID of 'shortname_tidy_display' and click the 'Add and edit' button. Now replace the existing code with that displayed below: -
Now you need to create the python script that will be called from near the bottom of the template (you will see it where
it says em tal:content="here/shortname_tidy")
In the ZMI go back into Portal Skins -> Custom and from the dropdown on the right create a 'script (python)'. Give it the id 'shortname_tidy' and click the 'Add and Edit' button.
Now remove any existing code and paste what is below in:-
# Example code: # function that takes a string and removes any spaces and underscores def shortname_tidy(string): newstring = string newstring = newstring.replace(' ','') newstring = newstring.replace('_','') newstring = newstring.lower(); return newstring # string to be changed test1 = ' Hello World_ hope you_are good ' return shortname_tidy(test1)
If you look at the code here there is a string called test1 with spaces and underscores in. This script calls the
shortname_tidy function, removes the whitespaces and underscores and returns the result to the calling page template
(shortname_tidy_display in this case).
-
Now go to your Plone site and call the page template - e.g, www.plone.org/shortname_tidy_display This will output the template with the tidied up python string - showing 'HelloWorldhopeyouaregood'.


