Dynamically Filled Fields or Visibility Chaning .cshtml In Projection

Topics: General
Sep 23, 2013 at 11:04 PM
Edited Sep 23, 2013 at 11:07 PM
Hello!

My blog posts require a link at the beggining of the post that looks like this:

Image

The link title is composed in this way: name_of_blog>title_of_post

As a first approach I created a Content-BlogPost.cshtml that looks like this:
@using Orchard.Utility.Extensions;
@{
    if (Model.Title != null) {
        Layout.Title = Model.Title;
    }

    Model.Classes.Add("content-item");
    
    var contentTypeClassName = ((string)Model.ContentItem.ContentType).HtmlClassify();
    Model.Classes.Add(contentTypeClassName);
    
    var tag = Tag(Model, "article");
}
@tag.StartElement
    <a href="/@Model.ContentItem.AutoroutePart.Path">@Model.Blog.Name>@Model.ContentItem.TitlePart.Title</a>
    <header>
        @Display(Model.Header)
        @if (Model.Meta != null) {
            <div class="metadata">
                @Display(Model.Meta)
            </div>
        }
    </header>
    @Display(Model.Content)
    @if(Model.Footer != null) {
        <footer>
            @Display(Model.Footer)
        </footer>
    }
@tag.EndElement
This added the link title that I needed, the problem is that in my home (wich is a projection of blog post) I can't show that title and I couldn't find a way to to wrap that line into an "if" statement to show it only where I need it. Is there any posibility to do something like:
@if(Model.Content.Parent.Name != "ProjectorPage")
{
<a href="/@Model.ContentItem.AutoroutePart.Path">@Model.Blog.Name>@Model.ContentItem.TitlePart.Title</a>
}
As I couldn't achieve the above functionality (if you know how to tell me) I started thinking about another posibility. It was creating a Link Field which obviously I can control where it gets displayed from the Placement.info file; and set the values of that Link Field from code (Url and text). This is more like an Automatic Filled Field, the user would never provide data for this field, it would get the data from the model in the view allowing me to show it and hide it where I need to. This possibility didn't succeeded neither because I couldn't access the link field from the Model.

Can you please tell me which of theses alternatives should I follow?

Is there another way to achieve what I need?

What should I correct/add to get any of these working?

Many thanks!
Coordinator
Sep 24, 2013 at 12:44 AM
Doing it in code is just fine. Is the problem that you can't access the "parent" of the model? Adding a field would be extremely convoluted, especially as you're going to want to set the value dynamically. One thing you can do however is create a shape and add it to the zone of your choosing. But you'd still need to determine when to do it.
If that is a problem only for your home page, why don't you simply alternate the template for url-homepage?
Sep 24, 2013 at 12:47 AM
You can attach a new part to blog post, and the part renders the field. In the driver of the part you can get the Blog Name, or I guess you can do that in the view file too. Then you can control the visibility with placement.info as you suggested.


Sep 25, 2013 at 4:17 PM
Edited Sep 25, 2013 at 9:17 PM
BertrandLeRoy wrote:
Doing it in code is just fine. Is the problem that you can't access the "parent" of the model? Adding a field would be extremely convoluted, especially as you're going to want to set the value dynamically. One thing you can do however is create a shape and add it to the zone of your choosing. But you'd still need to determine when to do it.
If that is a problem only for your home page, why don't you simply alternate the template for url-homepage?
What do you mean with "alternate the template for url-homepage"?
Should I alternate the blogpost when it is displayed in the url "/" or do you mean anything else? How would be the syntax to achieve this?

kassobasi wrote:
You can attach a new part to blog post, and the part renders the field. In the driver of the part you can get the Blog Name, or I guess you can do that in the view file too. Then you can control the visibility with placement.info as you suggested.
If you can be a little more specific about this I'd be really grateful!

Wich kind of part should I add to the blog post?
How do I make the part render the field?

Thanks for your answers guys!
Coordinator
Sep 25, 2013 at 9:42 PM
Sep 26, 2013 at 7:10 AM
Compared to Bertrand's suggestion to create an alternate for the homepage, my suggestion (creating a part -by writing a module- and attaching it to Blog Post content type -in the migration file of the module-, implementing drivers, views etc.) is unnecessary and complex. Please ignore it.


Sep 30, 2013 at 11:10 PM
I still don't fully understand Bertrand suggestion :(
Sep 30, 2013 at 11:44 PM
If you use shape tracing in your homepage, when you select the shape (blog post iirc), one of the alternates suggested by ths shape tracing is named something like Content-BlogPost-HomePage.cshtml (naming is described in the documentation link Bertrand posted) . If you add your markup to this alternate, it's only used when a blog post is rendered at home page. So in your case you want the markup to be shown in any other place than homepage, then you will do two alternates, on with general override (without homepage in the name, you already did it), and one with homepage in the name without the markup. I guess this is what he suggested.


Coordinator
Oct 1, 2013 at 1:44 AM
yes.
Oct 4, 2013 at 6:26 PM
kassobasi wrote:
If you use shape tracing in your homepage, when you select the shape (blog post iirc), one of the alternates suggested by ths shape tracing is named something like Content-BlogPost-HomePage.cshtml (naming is described in the documentation link Bertrand posted) . If you add your markup to this alternate, it's only used when a blog post is rendered at home page. So in your case you want the markup to be shown in any other place than homepage, then you will do two alternates, on with general override (without homepage in the name, you already did it), and one with homepage in the name without the markup. I guess this is what he suggested.
Absolutely awesome!

Many Thanks!!!!