Developer forum

Forum » Templates » razor loop through paragraph

razor loop through paragraph

Martin Kure
Reply

Hi, 

I'm using razor and I would like to loop through paragraphs on a page - a bit like I could do with with the template tag <div class="dwcontent" id="Nyhedssliderbox" title="Nyheds Slider"> 

Is there a way in razor, where you can loop through all the content within a paragraph?

What I'm trying to do specifically is create a counter on the number of active paragraphs i a placeholder on a page.

Regards

 

Martin


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Martin

Not sure I understand your question. What context are you in - which type of template?

On a page (layout template) you cannot loop paragraphs from razor. You have a content placeholder which will render a stack of paragraphs for you.

If the page is an item, you can loop any sub items of that item

If you are in a paragraph template you can render the item in that.

BR Nicolai

 
Martin Kure
Reply

Hi Nicolai, 

How do I loop through it as an item list - do you happen to have an example?

Regards

Martin

 
Martin Kure
Reply
 
Nicolai Høeg Pedersen
Reply

Hi Martin

You have to explain what you are trying to do.

If you have a page with a layout template, that template has content placeholders as you describe - that would be the same weather the layout template is razor or html.

So this is how your placeholder will look in your MyLayout.html file:

<div class="dwcontent" id="Nyhedssliderbox" title="Nyheds Slider">  

And this is how it looks in your MyLayout.cshtml file:

<div class="dwcontent" id="Nyhedssliderbox" title="Nyheds Slider"> 

Exactly the same.

The placeholder then gets filled with paragraphs attached to that placeholde on the page being rendered.

The paragraphs are rendered with their paragraph template - which again can be either html or cshtml. You can mix and match.

You might want to have a look at the Razor document found here:

http://developer.dynamicweb.com/downloads/documentation.aspx

Hope this clarifies.

BR Nicolai

 
Martin Kure
Reply

Hi Nicolai, 

 

thank you for your patience :-).

I have jumped a bit a head. I'm trying to build a slider, where I have made an item list, which contains images, caption text etc. 

Now I wanna iterate through these items in my item list.

So far I have this, but it gives me errors:

@{
var items = GetLoop("Item.Items");
var id = string.Format("carousel-{0}", GetValue("Item.Id"));

}
<p> @items </p>

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">

    @for (var i = 0; i < items.Count; i++) {
    var tabid = items[i];
        if (i == 0) {
        <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
        } else {
        <li data-target="#carousel-example-generic" data-slide-to="@i"></li>
        }
    }


  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner">
      @for (var i = 0; i < items.Count; i++) {
        var item = items[i];
        if (i == 0) {
            <div class="item active">
                  <img src="@item.GetValue(Item.Items.Billede)" alt="billede">
                  <div class="carousel-caption">
                      @item.GetValue(Item.Items.BilledTekst);
                  </div>
              </div>
        } else {
            <div class="item">
              <img src="@item.GetValue(Item.Items.Billede)" alt="billede">
              <div class="carousel-caption">
                @item.GetValue(Item.Items.BilledTekst);
              </div>
            </div>
        }
    }

  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
  </a>
</div>

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

It would be a great help if you told what the error is...

I still would suggest you read the document to get concepts straight.

Also look at https://github.com/dynamicweb/razor and https://github.com/dynamicweb/dynamicweb-templates which both contains information and sample templates.

Furthermore, attached find an example of a carousel (2 item types) and a template in Razor.

BR  Nicolai

Votes for this answer: 1
 
Martin Kure
Reply

Hi Nicolai, 

my apologies, I was in a bit of a rush, and didn't give you an in depth description of my problem. I'm sorry, especially now that you take your time giving such a fine support :-).

I get the following errors:

Line 30: The name 'Item' does not exist in the current context
Line 32: The name 'Item' does not exist in the current context
Line 37: The name 'Item' does not exist in the current context
Line 39: The name 'Item' does not exist in the current context

Which corresponds to this part of the code:

/*_______

  <!-- Wrapper for slides -->
  <div class="carousel-inner">
      @for (var i = 0; i < items.Count; i++) {
        var item = items[i];
        if (i == 0) {
            <div class="item active">
                  <img src="@item.GetValue(Item.Items.Billede)" alt="billede">
                  <div class="carousel-caption">
                      @item.GetValue(Item.Items.BilledTekst);
                  </div>
              </div>
        } else {
            <div class="item">
              <img src="@item.GetValue(Item.Items.Billede)" alt="billede">
              <div class="carousel-caption">
                @item.GetValue(Item.Items.BilledTekst);
              </div>
            </div>
        }
    }

  </div>

/*___________

It gives errors, whenever I'm trying to do something like this:   <img src="@item.GetValue(Item.Items.Billede)" alt="billede">.

Just for the record:

I'm trying to loop through an itemlist, isn't Item.Items they dynamicweb way to access the item list on the page, like so: var items = GetLoop("Item.Items");

Regards

Martin

 
Nicolai Høeg Pedersen
Reply

Hi Martin

You write  @item.GetValue(Item.Items.BilledTekst); it should be  @item.GetValue("Item.Items.BilledTekst"); You are missing the plings!

BR Nicolai

 
Martin Kure
Reply

Hi Nicolai, 

that got rid of the errors. But I'm not getting any iteration though.

Do you happen to have an example or doc that shows, how to iterate through an item list in DW razor. 

As I have understood it GetLoop("Item.Items") is in short:

Item = The Item on the page.

Items = the itemlist on the item.

One of the things, that I find hard to grasp right now is how to retrieve the information from DW. I have trouble figuring out, what to within the getLoop etc.

Thank you.

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Martin

In the post "Posted on 29/09/2014, 15:54" I made yesterday, you can find links to examples, documentation and a sample of a carousel implementation using 2 item types and a template that matches.

Again, it would help you a lot if you read the Razor document found here:

http://developer.dynamicweb.com/downloads/documentation.aspx

BR Nicolai

Votes for this answer: 1
 
Martin Kure
Reply

Thank you very much, Nicolai. 

 

You must be logged in to post in the forum