9

For every Page I want to write out a meta tag in the HTML head containing the full Page URL. How do I build up this URL including domain name?

We're using SDL Tridion 2011 SP1 with modular templates.

13

I'm assuming you're attempting to obtain the URL within your Template Building Block.

If you've got a Page object, you can simply call the following Page.PublishLocationUrl to give you something like /mydirectory/mypage.html.

The domain isn't stored, you're probably best using server side code to output the host name, if you're using static pages you can simply store the website URL somewhere in SDL Tridion (I recommend as metadata on the publishable Publication. You can read that value using the API from the Page.Publication.Metadata

Here's a bit more code to show getting the page and reading the page URL:

In your TBB:

    Page page = null;
    Item pageItem = package.GetByType(Package.PageName);
    if (pageItem != null)
    {
        page = engine.GetObject(pageItem.GetAsSource().GetValue("ID")) as Page;
    }

That will get you your page object.

You can then read the PublishURL from it:

string pagePath = page.PublishLocationUrl;

Once you've constructed your full path (including domain however you wish to manage that) you can then push the data in the package, which can be ready by your page DWT layout file :)

  • 1
    I'll be nitpicky :) just because I like to tease @wntr. I'd recommend always using package.GetByName(Package.PageName) instead of GetByType: you can have multiple pages in the same package (rare but possible) and GetByType will get you the last page in the stack. – Nuno Linhares Mar 2 '13 at 8:43
  • 1
    GetByType will get the item of the type you request that was most recently added to the package. GetByName will get the item with that name that was most recently added to the package. So no difference there. The difference is that you can (and should) use a non-standard name (so not "Page", but e.g. "ReferingPage") for any Page you decide to add to the package, where most likely you will want to use the default type (tridion/page). So looking up a page by its name is more likely to give you the one you are looking for, even when other developers later add their pages to the package too. – Frank van Puffelen Mar 3 '13 at 2:16
  • hahah thanks chaps :) - i'll update my question – johnwinter Mar 5 '13 at 23:10
8

A humble tip is to install and use Razor Templating, which gives you near-full access to the .NET back-end of your templates. In your case you can simply write @Publication.PublicationUrl. This can be done inside you Page Template Building Block, without the need for custom TBB's.

5

Without knowing the full requirements, it is difficult to give a definitive answer on this. However, if you are just after the URL in a HTML comment on the page then you may want to consider doing this in your ASP.NET / JSP code rather than your Tridion template**.

e.g. <meta url="<%=Request.Url.AbsoluteUri %>" />

Although this code would need to be executed at request time (which could be offset with caching), it would have a number of benefits including:

  1. You wont have to handle the differences between you Staging and Live websites in your Tridion code.
  2. You wont have to have different settings in each of your Tridion environments (making Content Ports and Database Refreshes simpler)
  3. Your site could be accessed using more than one URL and still display the correct url (you may need to consider your caching strategy with this)
  4. You can also get the querystring of the URL used to reference the page

**I am aware that the question was specifically about doing this in a Page Template, but it is always worth considering alternative options.

4

In a Page Template there is no option to get the domain name of the website this page is Published to, since SDL Tridion doesn't know this information. Content (including Pages) is published to a Publication Target, which is in SDL Tridion terms just a destination. How this is exposed as a website (and with what domain/url) is not known.

What you can do is add metadata to for instance the Publication, containing the url (or domain name) for your live (and optionally staging) websites. Then you can have your Page Template pick up this information and build the URL from that (adding the Page path and filename + extension).

3

If you want to keep information about the target web site in the Tridion CM, the logical approach is to associate it with the publication target. In the past, I have done this by adding a field to the protocol schema, however these days you might prefer to use ApplicationData

3

In regards to point 2 of Jonathan and for the sake of storing information in the right place i suggest to put domainnames in the right place, there where it is managed, at the CD side in the same config as we configure the site settings. But this means that you will have to write a Deployer extention to retrieve that during deploy and place that in the page. I would say a bit of overkill for what you wish to accomplish :)

Reading your question there is no mention you actually want to do this in your tridion templates, you only mention what kind of templates you use. This makes Jonathans answer the most viable in my eyes, unless your pages need to be HTML only or your cashing rules are very strict. In that case i would re-consider your businesscase requirements as a HTML only website is not so much nessesary anymore these days :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.