Copied to clipboard

Flag this post as spam?

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


  • Nicolai 48 posts 201 karma points
    Oct 22, 2015 @ 13:34
    Nicolai
    0

    Change content based on macro parameter?

    Hey guys!

    Im trying to figure out why im not allowed to change a variable containing the nodes i want to display based on a macro parameter.

    This is how it looks:

        var number = Model.MacroParameters["ListType"]; 
        int numType = Convert.ToInt32(number);
    
        @numType
        if (numType == 1) {
            var allArticles = CurrentPage.AncestorOrSelf(1).Descendants("article").OrderBy("createDate desc");
        } else if (numType == 2) {
            var allArticles = Umbraco.Content(1098);
        }
    

    When I am trying to do it like this I get an "Error loading Partial View script" message. I know that im getting the correct value in numType.

    I hope someone can help me with this!

    Note: Below these lines of code i loop through allArticles to get each article.

  • Carl Jackson 139 posts 478 karma points
    Oct 22, 2015 @ 15:23
    Carl Jackson
    0

    You can't set var allArticles inside an if like that

    you are also trying to set it to two differnt types

    The top will return an IEnumerable of dynamic content and the bottom just a single dynamic content item.

    if you want all children of content item 1098 you should be able to do the following

     var number = Model.MacroParameters["ListType"]; 
    int numType = Convert.ToInt32(number);
    
    
       var allArticles = Umbraco.Content(1098).Children();
    
    if (numType == 1) {
       allArticles = CurrentPage.AncestorOrSelf(1).Descendants("article").OrderBy("createDate desc");
    } 
    
  • Nicolai 48 posts 201 karma points
    Oct 22, 2015 @ 18:32
    Nicolai
    0

    I know about the two different types, it was just a quick sketchup :)

    Im unfortunately not interested in all articles of '1098'. I want to change allArticles based on the macro parameter.

    It is because im calling the above partial view macro from 3 different template views. Each view is sending a different parameter value (1, 2 or 3). Then I need to - based on the value I fetch - display different content. I therefore need to change the content I want to loop through aka. allArticles.

    Does it make sense? :D

  • Carl Jackson 139 posts 478 karma points
    Oct 23, 2015 @ 07:09
    Carl Jackson
    0

    Yes makes sense and should be possible.

    As well as the type of the variable the scooe is also not valid through do you know this too?

    Can you post your actual code ? Or an error message. You can turn errors on for macros in one if the config files by setting the value to throw. Not at a computer so not 100% sure what right now.

    Thanks

    Carl

  • Nicolai 48 posts 201 karma points
    Oct 23, 2015 @ 08:13
    Nicolai
    0

    Template1, Template2 and Template3 Code:

    The value of ListType is different in each template view.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "MainTemplate.cshtml";
    
        Dictionary<string,object> passListType = new Dictionary<string,object>();
        passListType.Add("ListType", 1);
    }
            <div id="main-wrapper">
                <div class="wrapper style3">
                    <div class="inner">
                        <div class="container">
                            <div class="row">
                                    @Umbraco.RenderMacro("showList", passListType)
                                    @{ Html.RenderPartial("epage"); }
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    

    Partial View Macro Code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var number = Model.MacroParameters["ListType"]; 
        int numType = Convert.ToInt32(number);
    
        var allArticles = CurrentPage.AncestorOrSelf(1).Descendants("article").OrderBy("createDate desc");
    
    @*  if (numType == 1) {
            var allArticles = CurrentPage.AncestorOrSelf(1).Descendants("article").OrderBy("createDate desc");
        } 
        @CurrentPage.Name *@
        <!-- Main Wrapper -->
        <div class="8u 12u(mobile)">
            <section class="box article-list">
                <h2 class="icon fa-file-text-o">@CurrentPage.title</h2>
                    @foreach (var article in allArticles) {
                            <article class="box excerpt">
                                <a href="@article.Url">
                                @if (@article.HasValue("articleType"))  {
                                    var articleType = Umbraco.GetPreValueAsString(@article.GetPropertyValue<int>("articleType"));
    
                                    if (articleType == "Billede Midt") {
                                        @:<div class="imgMid">
                                    } else if (articleType == "Billede Venstre") {
                                        @:<div class="imgLeft">
                                    } else if (articleType == "Billede Højre") {
                                        @:<div class="imgRight">
                                    } else if (articleType == "Intet Billede") {
                                        @:<div class="noImg">
                                    }
    
                                            <div class="articleImage">
                                                <div class="img">
                                                    @if (@article.HasValue("articleImage")) {
                                                        <span class="image featured">
                                                            <img src="@Umbraco.Media(article.articleImage).Url" alt="" />
                                                        </span>
                                                    }   
                                                </div>
    
                                                <div class="articleDate">
                                                    <p style="text-transform: capitalize;">@article.articleDate.ToString("M", new System.Globalization.CultureInfo("da-DK"))</p>
                                                </div>
                                            </div>
    
                                            <div class="articleText">
                                                <header>
                                                    <h3>@article.articleTitle</h3>
                                                </header>
                                                @if (@article.articleBodytext.ToString().Length > 280) {
                                                    @Html.Raw(@article.articleBodytext.ToString().Substring(0, 180))<text>...</text>
                                                } else {
                                                    @Html.Raw(@article.articleBodytext.ToString())
                                                }
                                            </div>
                                        @:</div>
                                }
                            </a>
                        </article>
                    }
            </section>
        </div>
    }
    

    Macro Parameter Page (Umbraco Backend): enter image description here

  • Carl Jackson 139 posts 478 karma points
    Oct 23, 2015 @ 08:52
    Carl Jackson
    0

    Hi Nicolai

    I can see a few issues with your view that will causue errors

    in the commented out "if" statemnet you are redeclaring the "allArticles" variable. If you uncomment this it will cause the partial to error it would need to change to just "allArticles =" and stay declared outside the if.

    Also as Razor is html aware the opening container which you have wrapped in a if statement will cause an error. you need to use Html.Raw() for this kind of output.

    Finally in @ and @{} are for outputting and opening code blocks respectively.

    You don't need @if(@variableName = "value") it should jsut be @if(variableName = "value")

    I've tried to make a working version of your code below - give it a go, if it fails can you try and get an error from the log or changing your config as I suggested above.

    Thanks

    Carl

    @{
    var number = Model.MacroParameters["ListType"];
    int numType = Convert.ToInt32(number);
    
    var allArticles = CurrentPage.AncestorOrSelf(1).Descendants("article").OrderBy("createDate desc");
    
    }
    
        @CurrentPage.Name 
    <!-- Main Wrapper -->
    <div class="8u 12u(mobile)">
        <section class="box article-list">
            <h2 class="icon fa-file-text-o">@CurrentPage.title</h2>
            @foreach (var article in allArticles)
            {
                <article class="box excerpt">
                    <a href="@article.Url">
                        @if (@article.HasValue("articleType"))
                        {
                            var articleType = Umbraco.GetPreValueAsString(@article.GetPropertyValue<int>("articleType"));
    
                            switch ((string)articleType.ToString())
                            {
                                case "Billede Midt":
                                    @Html.Raw("<div class=\"imgMid\">")
                                    break;
                                case "Billede Venstre":
                                    @Html.Raw("<div class=\"imgLeft\">")
                                    break;
                                case "Billede Højre":
                                    @Html.Raw("<div class=\"imgRight\">")
                                    break;
                                case "Intet Billede":
                                    @Html.Raw("<div class=\"noImg\">")
                                    break;
                            }
    
                                <div class="articleImage">
                                    <div class="img">
                                        @if (article.HasValue("articleImage"))
                                        {
                                            <span class="image featured">
                                                <img src="@Umbraco.Media(article.articleImage).Url" alt="" />
                                            </span>
                                        }
                                    </div>
    
                                    <div class="articleDate">
                                        <p style="text-transform: capitalize;">@article.articleDate.ToString("M", new System.Globalization.CultureInfo("da-DK"))</p>
                                    </div>
                                </div>
    
                                <div class="articleText">
                                    <header>
                                        <h3>@article.articleTitle</h3>
                                    </header>
                                    @if (article.articleBodytext.ToString().Length > 280)
                                    {
                                        @Html.Raw(@article.articleBodytext.ToString().Substring(0, 180))<text>...</text>
                                    }
                                    else
                                    {
                                        @Html.Raw(article.articleBodytext.ToString())
                                    }
                                </div>
                            @Html.Raw("</div>")
                        }
                    </a>
                </article>
            }
        </section>
    </div>
    
  • Nicolai 48 posts 201 karma points
    Oct 23, 2015 @ 10:00
    Nicolai
    0

    You're still not changing allArticles based on the macro parameter.

    I need to change the specific nodes I want to loop through. Like this:

    @{
        var number = Model.MacroParameters["ListType"];
        int numType = Convert.ToInt32(number);
    
        if (numType == 1) {
            var allArticles = (Some DynamichPublishedContentList)
        } else if (numType == 2) {
            var allArticles = (Some other DynamichPublishedContentList)
        } else 
            var allArticles = (Some other DynamichPublishedContentList)
        }
    }
    ...
    for each (var article in allArticles ) {
        ....
    }
    

    That is my only problem. Im sorry if im not explaining it well-enough. And thank you for trying to figure it out!

  • Carl Jackson 139 posts 478 karma points
    Oct 23, 2015 @ 10:13
    Carl Jackson
    100

    As my post above you can't use the allArticles variable in that scope!

    It must be declared outside the if statement and then set inside.

    Example, look at how allArticles is declared and set :

    @{
    dynamic allArticles;
    
    var number = Model.MacroParameters["ListType"];
    int numType = Convert.ToInt32(number);
    
    if (numType == 1)
    {
        allArticles = Umbraco.Content(1);
    }
    else if (numType == 2)
    {
        allArticles = Umbraco.Content(2);
    }
    else
    {
        allArticles = Umbraco.Content(3);
    
    }
    

    }

  • Nicolai 48 posts 201 karma points
    Oct 23, 2015 @ 11:51
    Nicolai
    1

    Now I get it! Why didnt you just use that example in the first post? ;)

    And its actually a really simple solution, doh.

    Thanks a ton for helping me out mate!

Please Sign in or register to post replies

Write your reply to:

Draft