Copied to clipboard

Flag this post as spam?

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


  • Dennis Nyström 4 posts 54 karma points
    Sep 22, 2014 @ 18:52
    Dennis Nyström
    0

    Add markup to <head> by macro in article/body (v4.71)

    Hi all,

    I'm having a difficult time figuring this one out.

    Here's the scenario:

    • I have a macro which lists x items in a pagelist (added by the editor)
    • It also supports/adds pagination ("< Prev page 1 2 3 ... Next Page >")
    • We now wish to add <link rel="prev/next" href="[url]"> tags in <head> based on pagination logic in the macro script.
    • Templates are masterpages/webform based.

    Could I somehow add an old-school control as global container (<asp:Literal id="litHeadMarkup".../>) which I can access and modify from a razor macro? Or maybe another (preferable nicer) alternative? 

    Any thoughts are highly appreciated.
    Thanks a bunch,
    Dennis

     

     

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Sep 23, 2014 @ 09:36
    Ismail Mayat
    0

    Dennis,

    You could do head runat=server then in the razor access that and add new literal control with the link?

    Regards

    Ismail

  • Dennis Nyström 4 posts 54 karma points
    Sep 23, 2014 @ 10:47
    Dennis Nyström
    0

    Ismail,

    Thanks for your response. I've added the runat=server to the head but I don't seem to access it from the razor. Maybe I do something wrong. Would you mind provide some brief example code for the razor script accessing the <head runat="server" id="MasterPageHeader">?

  • Dennis Nyström 4 posts 54 karma points
    Sep 23, 2014 @ 14:05
    Dennis Nyström
    100

    I got it, at last!

    My solution:

    Added a plain Literal-control to my root masterpage (and removed runat=server from the <head>):

    <head>
     ...
      <asp:Literal ID="litHeadMarkup" runat="server" />
    </head>
    

    I accessed the control from my razor script by doing:

    @using umbraco.MacroEngines;
    @using System.Web.UI.WebControls;
    @{ 
        Page currPage = (Page)HttpContext.Current.Handler;
        MasterPage rootMaster = currPage.Master;
    
        //find the root masterpage
        while(rootMaster.Master != null){ rootMaster = rootMaster.Master; }
    
        var firstContentPlaceHolder = (ContentPlaceHolder)rootMaster.FindControl("ContentPlaceHolderDefault");
        if(firstContentPlaceHolder != null)
        {
           Literal headMarkup = (Literal)firstContentPlaceHolder.FindControl("litHeadMarkup");
           headMarkup.Text = "<!-- this value will be inserted to <head></head> -->";
        }
    }
    

    Hope it helps anybody else looking for this.

    And thanks for your inputs!

  • Simon 692 posts 1068 karma points
    Apr 17, 2015 @ 22:44
    Simon
    0

    Hi all,

    Do you have any idea how can I achieve this through MVC razor, since I am not using web forms?

    I want that in a macro, I set the canonical url next/prev in the head section pls.

    Thank you.

    Kind Regards

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Apr 17, 2015 @ 23:04
    Morten Bock
    0

    @Simon Wouldn't you be able to use the ViewBag for that?

  • Simon 692 posts 1068 karma points
    Apr 17, 2015 @ 23:08
    Simon
    0

    Hi Morten,

    How can I achive this, using ViewBag? Can you please give me an example?

    Thank you very much

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Apr 18, 2015 @ 00:09
    Morten Bock
    0

    Sure... It's basically ViewBag 101:

    In your controller action

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";
    
        ViewBag.NextHref = "http://example.com/foo?page=2";
    
        return View();
    }
    

    In your _Layout file

    <head>
        <title>@ViewBag.Title - My ASP.NET Application</title>
    
        @{
            var nextHref = ViewBag.NextHref;
        }
        @if (nextHref != null)
        {
            <link rel="next" href="@ViewBag.NextHref"/>
        }
    </head>
    
  • Simon 692 posts 1068 karma points
    Apr 18, 2015 @ 08:34
    Simon
    0

    Hi Morten,

    Preferabbly I do not want to modify the umbraco controllers.

    Moreover, I want to set the <link> in the head section from a Macro partial, from where the pagination logic is located.

    Any ideas pls?

     

    Thank you.

     

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Apr 18, 2015 @ 09:03
    Morten Bock
    0

    What have you tried so far?

    Have you tried setting the ViewBag.NextHref from your MacroPartial?

  • Simon 692 posts 1068 karma points
    Apr 18, 2015 @ 09:26
    Simon
    0

    So what are you saying is to set a ViewBag.NextHref value in the Macro Partial and get the Value of this Viewbag in the head section in the MasterPage?

    Thank you.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Apr 18, 2015 @ 09:27
    Morten Bock
    0

    Yes.

  • Simon 692 posts 1068 karma points
    Apr 18, 2015 @ 11:09
    Simon
    0

    No success.

    Nothing is being shown in the head.

Please Sign in or register to post replies

Write your reply to:

Draft