pythonsubdomainplonetemplate-talzpt

Save a subdomain name to String


It's a rather unusual request, but is it possible to extract a subdomain to a variable?

e.g.
(1)  sub1.mydomain.com
(2)  sub2.mydomain.com 

When I click on (1) I want to save "sub1" and vice versa. I use plone (python and tal). Thx for your input.


Solution

  • Just use a Python expression to split at the first dot:

    tal:define="subdomain python:domain.partition('.')[0]"
    

    or, if using Python 2.4 or earlier:

    tal:define="subdomain python:domain.split('.', 1)[0]"
    

    This uses str.partition() or str.split() to return a list of strings; the local name is the first part; [0] selects the first element of that list.

    Demo using a Python prompt:

    >>> 'sub1.mydomain.com'.partition('.')[0]
    'sub1'
    >>> 'sub1.mydomain.com'.split('.', 1)[0]
    'sub1'