Copied to clipboard

Flag this post as spam?

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


  • Niels Pedersen 18 posts 49 karma points
    Mar 23, 2012 @ 09:40
    Niels Pedersen
    0

    Showing specific page from contentpicker

    Hi guys

    I have been struggling with a Razor script for many hours now, and finaly admits that i need help :)

    The script is a "Info-box" that will be shown in the right side of a text-page. The user have the option to choose to show the box or not (true will show a random box) or choose from the content-picker a specifix info-box on a specific textpage. I have figured out how to show the random info-boks if you check it to true, but the specific info-box is really troubling me.

    A brief explenation of the content in the code:

    "visBoks" = the truefalse checkbox to show a random info-box. (Generic prop on the Textpage doctype)

    "Model.NodeByID(1362)" = The ID refers to the container(in the content) of the infoboxes.

    "vlgInfoboks" = The Content-picker that i use to choose a specific info-boks to show (Generic prop on the Textpage doctype)

    The page.visteDuOverskrift (etc..) is generic properties that creates the content on the specific info-box

    This is what i got so far:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @if (Model.visBoks == true{
          foreach (var page in Model.NodeById(1362).Children.Random(1){
            
            <div class="block_box_i">
                <h2>Viste du at..</h2>
                  <p>@page.visteDuOverskrift</p>
                  <class="more_link_s" href="@page.linkTilSideVisteDu">@page.visteDuLinktekst</a>
            </div>
            
    }
    @if (Model.vlgInfoboks == "true"{
          foreach (var page in Model.NodeById(1362).Children(1).Where(@Model.Id == @Model.vlgInfoboks){
            
            <div class="block_box_i">
                <h2>Viste du at..</h2>
                  <p>@page.visteDuOverskrift</p>
                  <class="more_link_s" href="@page.linkTilSideVisteDu">@page.visteDuLinktekst</a>
            </div>
            
    }

    else {
        
                
         }

    I'm all new to coding in general, so I really appreciated all the help i can get

  • Niels Pedersen 18 posts 49 karma points
    Mar 23, 2012 @ 09:49
    Niels Pedersen
    0

    When i run the script it works perfect when i visBoks checkbox to true. But when i choose a specific info-box from the content-picker it dosnt show anything.

    (Got a xslt-error when trying to edit the above post)

    Here is how the content is structured:

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 23, 2012 @ 13:30
    Tom Fulton
    0

    Hi,

    To grab a specific node you the Library.NodeById(nodeId) helper.  That said, something like this would probably work.  Note I also moved your infobox div into a helper to avoid duplication :)

    @{
    if (Model.visBoks == true && !Model.HasValue("vlgInfoboks")) {
          var randomPage = Model.NodeById(1362).Children.Random(1);
          @ShowInfoBox(randomPage);
    }
    else if (Model.visBoks == true && Model.HasValue("vlgInfoboks")) {
        var page = Library.NodeById(Model.vlgInfoboks);
        @ShowInfoBox(page);
    }
    }
    @helper ShowInfoBox(dynamic page) {
         <div class="block_box_i">
                <h2>Viste du at..</h2>
                  <p>@page.visteDuOverskrift</p>
                  <a class="more_link_s" href="@page.linkTilSideVisteDu">@page.visteDuLinktekst</a>
            </div>
    }

    -Tom

  • Niels Pedersen 18 posts 49 karma points
    Mar 23, 2012 @ 14:29
    Niels Pedersen
    0

    Hi Tom

    Thank you very much! It was a great help and i finaly got it working.

    Your code didn't work just by cpoy/paste. There where som problems with the definition og "page". So i had ditch the @helper (although a very cool feature) and bring back the mess. I know it's not pretty, but this got the whole thing working:

    @inherits umbraco.MacroEngines.DynamicNodeContext


    @{
    if (Model.visBoks == true{

          foreach (var page in Model.NodeById(1362).Children.Random(1){
            
            <div class="block_box_i">
                <h2>Viste du at..</h2>
                  <p>@page.visteDuOverskrift</p>
                  <class="more_link_s" href="@page.linkTilSideVisteDu">@page.visteDuLinktekst</a>
            </div>
            
    }
      
      
      
    else if (Model.HasValue("vlgInfoboks"){
        var page Library.NodeById(Model.vlgInfoboks);
        <div class="block_box_i">

                <h2>Viste du at..</h2>

                  <p>@page.visteDuOverskrift</p>

                  <class="more_link_s" href="@page.linkTilSideVisteDu">@page.visteDuLinktekst</a>

            </div>
    }
    }

     

    Again, thanks for all you help :)

Please Sign in or register to post replies

Write your reply to:

Draft