1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
dustinboettcher
  • 495
  • 1
  • 6
  • 19

1 Answers1

5

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'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Watch out for your Plone version. Python 2.4 (used by Plone 3) will not provide you `.partition`. In that case use `'sub1.mydomain.com'.split('.', 1)[0]` – keul Feb 09 '14 at 16:19
  • @keul: excellent point; I tend to forget that `.partition()` was added in 2.5. – Martijn Pieters Feb 09 '14 at 17:32
  • worked like a charm well I used self.request["URL"] to obtain the url but the rest was fine - thx to both of you – dustinboettcher Feb 10 '14 at 16:34