Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    Jul 06, 2012 @ 09:04
    Anthony Candaele
    0

    make id attribute dynamic in body tag

    Hi,

    I have a style sheet that has some different style definitions for the layout of several different pages:

     

    /* ============================= page2 ===========================*/

    #page2 .col-2 .col-1 {width:230px;}

    #page2 .col-2 .col-2 {width:230px;}

    /* ============================= page3 ===========================*/

    #page3 .col-2 .col-1 {width:280px;}

    #page3 .col-2 .col-2 {width:219px;}

    /* ============================= page5 ===========================*/

    #page5 .col-2 .col-1 {width:260px;}

    #page5 .col-2 .col-2 {width:239px;}

     

    the different id selectors (#page2, #page3, etc ...) are then added to the body-tag of the page:

    <body id="page3">

    My question is, how can I make this dynamic in an Umbraco Masterpage? My first thought is to create a documenttype property, say 'layoutId' and then add this to the Masterpage template:

    <body id="<umbraco:Item field='layoutId' runat='server' />">

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 06, 2012 @ 09:24
    Fuji Kusaka
    1

    Hi Anthony,

    How about if you create a custom DataType DropDown List then add the values in this case the ids (page2, page3, etc... ) , since you have a the same value for page 2 you could add the following

     <umbraco:Item field="layoutId" useIfEmpty="page2" recursive="true" runat="server" />

    Not sure if this will solve your issue thought, i did that for a project of mine there the client could have the possibility to change the background color of this website.

    //fuji

  • Anthony Candaele 1197 posts 2049 karma points
    Jul 06, 2012 @ 10:02
    Anthony Candaele
    0

    Hi Fuji,

    That's indeed a better solution than having a doctype property of datatype Text. Still cumbersome a bit is that the end user has to define which style to use. The ultimate solution of course would be that the Masterpage "just knows" wich layout style to use, based for instance on the Template used.

    thanks for the advice,

    Anthony

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 06, 2012 @ 10:50
    Michael Latouche
    0

    Hi Anthony,

    If you want to auto-set the style based on the template, maybe you can do something like this:

    <body class="<umbraco:Itemfield="Template"useIfEmpty="defaultStyle"runat="server"/>">

    and then rename your stylsheet "#pageX" classes by ".TemplateName"? I'm not sure about the name of the field property returning the template...

    In that case, if the user changes the template, the style will change automatically.

    Cheers,

    Michael.

  • Anthony Candaele 1197 posts 2049 karma points
    Jul 06, 2012 @ 11:06
    Anthony Candaele
    0

    Hi Michael,

    Thanks for the advice. I don't know if there is an Itemfield for a Template. If there is, that would solve the problem of dynamically loading the correct id selector for the body-tag.

    greetings,

    Anthony

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 06, 2012 @ 11:22
    Michael Latouche
    1

    Hi Anthony,

    I'm not sure eithet, but if does not exists, it definitely exists in the razor dynamic node model (see cheat sheet), so worse case you might use inline razor I guess.

    Cheers,

    Michael.

  • Anthony Candaele 1197 posts 2049 karma points
    Jul 06, 2012 @ 14:03
    Anthony Candaele
    0

    there is a @template field item, but this returns the id of a template, not it's Name. I'm now looking into the Razor Cheatsheet. It says that the DynamicNode has a .Template property, but I'm not quiet sure how to access that property

    anyone accessed the Template name in a Razor file?

    Thanks for your help,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Jul 06, 2012 @ 15:00
    Anthony Candaele
    0

    I found a part of the solution here:

    http://our.umbraco.org/forum/developers/xslt/1764-testing-against-active-template-name-%5Bsolved%5D

    but I use this method in my template:

    <%umbraco.cms.businesslogic.template.Template.GetTemplate(1055) %>

    I get this result:

    <body id="{Id:1055, Text:Homepage, ParentId: -1}">

    While I just want the name of the template (eg. Homepage) so the result is this:

    <body id="Homepage">

    I know I'm close, I just need this little push from the Umbraco Communit

    Thanks,

    Anthony

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 06, 2012 @ 15:08
    Michael Latouche
    0

    Maybe call the GetProperty method:

    <%umbraco.cms.businesslogic.template.Template.GetTemplate(1055).GetProperty("Text") %>

    Cheers,

    Michael.

  • Anthony Candaele 1197 posts 2049 karma points
    Jul 06, 2012 @ 15:13
    Anthony Candaele
    0

    Hi Michael,

    tried it but this doesn't work, I get an YSOD:

    'umbraco.cms.businesslogic.template.Template' does not contain a definition for 'GetProperty' and no extension method 'GetProperty' accepting a first argument of type 'umbraco.cms.businesslogic.template.Template' could be found (are you missing a using directive or an assembly reference?)

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 06, 2012 @ 15:27
    Michael Latouche
    1

    Mmmh too bad. I don't have access to code here but there must certainly be a GetValue method or something like this... Or could it be that you just have a ".Text" property. Pff, I don't remember... Hope somebody else does.

     

  • Anthony Candaele 1197 posts 2049 karma points
    Jul 06, 2012 @ 15:46
    Anthony Candaele
    0

    jep, found it:

    <%umbraco.cms.businesslogic.template.Template.GetTemplate(1055).Text %>

    Thanks a lot Michael

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 06, 2012 @ 15:53
    Michael Latouche
    0

    OK Great :-)

  • Anthony Candaele 1197 posts 2049 karma points
    Jul 06, 2012 @ 16:03
    Anthony Candaele
    0

    to recap, what I did to add the name of an Umbraco template dynamically in the body tag of my Masterpage, I did the following:

    1. In the body tag of my masterpage I defined a contentplaceholder like this:

    <body id="<asp:ContentPlaceHolder Id="ph_body" runat="server" />">

    2. In the Content Template, for instance the Homepage template, I added a content region like this:

    <asp:Content ContentPlaceHolderId="ph_body" runat="server">
    <%umbraco.cms.businesslogic.template.Template.GetTemplate(1055).Text %>
    </asp:Content>

    The result in the rendered page is this: 

    <body id="
      Homepage
     ">  

    So I just need to find a way to tidy up the output a little and clean up the spaces

  • Anthony Candaele 1197 posts 2049 karma points
    Jul 09, 2012 @ 09:15
    Anthony Candaele
    0

    I was close to a solution putting this in the body tag of my Master page:

    <body id="<%= umbraco.cms.businesslogic.template.Template.GetTemplate(1055).Text %>" />

    However, I didn't find a way to inject a dynamic value in the GetTemplate(template id) method, so I think in the end Fuji's solution is the most practical, so I will mark that solution as the solving one.

    However, I hope in the future it will be more easy to dynamically inject a template name in the body tag of a Master page.

    greetings,

    Anthony

Please Sign in or register to post replies

Write your reply to:

Draft