i try without luck to insert 6 images into 2 lines,
the problem is that the line number 2 break the last 2 images and push them down.
i'm using Umbraco and bootstrap 3.
here is my code
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = "InnerPage.cshtml";
}
#section slider
{
#{Html.RenderPartial("ImageSlider");}
}
#{
<!-- PROGRAM PAGE -->
<script src="js/script.js"></script>
<div class="caption-button caption-gray left-top" style="font-size:14px;">UPCOMING EVENTS</div>
<div class="padding-top-60">
<div class="row row-lg-height">
<div id="eventCarousel" class="carousel slide vertical" >
<div class="carousel-inner">
#{
var content = Umbraco.Content(1122);
int itemsCount = content.Children.Count();
int sliders = itemsCount / 6;
for (int i = 0; i <= sliders; i++)
{
var items = content.Children;
if (i == 0)
{
#Html.Raw("<div class=\"item active\">")
items = content.Children.Take(6);
}
else
{
items = content.Children.Skip(i * 6).Take(6);
#Html.Raw("<div class=\"item\">")
}
foreach (var childContent in items)
{
var contentService = ApplicationContext.Current.Services.ContentService.GetById(int.Parse(childContent.Id.ToString()));
var title = childContent.Name.ToString();
var image = Umbraco.Media(contentService.Properties["Thumbnail"].Value.ToString());
var description = contentService.Properties["shortDescription"].Value.ToString();
var img = image.Url.ToString();
<div class="col-lg-4">
<img src="#image.Url" class="media-object pull-left img-responsive" />
<h1 class="media-heading" style="color:#606060;">#title</h1>
<div style="padding:0px 5px; ">#MvcHtmlString.Create(#description)</div>
</div>
}
#Html.Raw("</div>")
}
}
</div>
</div>
</div>
</div>
<a class="caption-button caption-red right-bottom" href="#eventCarousel" data-slide="next">MORE</a>
}
i attached 2 images so you can see what firebug showing to me and what happened on the screen.
PLEASE HELP ME ! I am breaking my head 2 days already and it's wasting my time..
What do i do wrong ??
I think your code is a littlebit difficult. If you use another approach you will be able to debug much easier.
Try some approach like this:
#{
var allNodesToLoop = Umbraco.Content(1122).Children;
}
#foreach(var nodegroup in allNodesToLoop.InGroupsOf(2) {
<div class="row">
#foreach(var item in nodegroup) {
<div class="col-md-4">
#item.Name
<!-- and other stuff you want to render in this grid cell -->
</div>
}
</div>
}
Related
I'm using wordpress to set up a website. When putting in a specific block of code wordpress for seemingly no reason will add additional HTML into my code that screws up the formatting. I've looked it over and found no noticeable errors that would cause that behavior. This is the relevant bit of code (I've included most of it as I don't know where the issue actually lies)
<div class = "section">
<script type="text/javascript">
var imageCount = 0;
var position = 0;
var isStopped = false;
$( document ).ready(function() {
imageCount = document.getElementsByClassName("rotator").length; //Pull all elements into an array and count the number to get the count.
//setButtons();
});
function nextImage() {
var rotationNumber = $('#imgGal').width() * imageCount; //determines when it should cycle back to the beginning
position += $('#imgGal').width();//position change to width of the element
if (position >= rotationNumber) //cycle once the end is reached
{
position = 0;
}
//jquery to animate the transition
$( ".rotator" ).animate({
right: position
}, 1000 );
}
function previousImage() {
var rotationNumber = $('#imgGal').width() * imageCount; //determines the end
if (position <= 0) //cycle once the beginning is reached
{
position = rotationNumber - $('#imgGal').width();
}
else{
position -= $('#imgGal').width();//position change to width of the element
}
//jquery to animate the transition
$( ".rotator" ).animate({
right: position
}, 1000 );
}
</script>
<div id = "testimonials">
<a class = "controls" onclick="previousImage()"><div class = "arrow"><</div></a><!--
--><div id = "imgGal">
<div class = "rotator">
<div class = "blurb">
<h3>John Doe</h3><p>"Positive Testimonial"</p>
</div>
</div><!--
--><div class = "rotator">
<div class = "blurb">
<h3>John Doe the Second</h3><p>"Positive Testimonial"</p>
</div>
</div>
</div><!--
--><a class = "controls" onclick="nextImage()"><div class = "arrow">></div></a>
</div>
</div>
</div>
And this is what it looks like after being published
<div class="section">
<script type="text/javascript">
var imageCount = 0;
var position = 0;
var isStopped = false;
$( document ).ready(function() {
imageCount = document.getElementsByClassName("rotator").length; //Pull all elements into an array and count the number to get the count.
//setButtons();
});</p>
<p> function nextImage() {
var rotationNumber = $('#imgGal').width() * imageCount; //determines when it should cycle back to the beginning
position += $('#imgGal').width();//position change to width of the element
if (position >= rotationNumber) //cycle once the end is reached
{
position = 0;
}</p>
<p> //jquery to animate the transition
$( ".rotator" ).animate({
right: position
}, 1000 );
}
function previousImage() {
var rotationNumber = $('#imgGal').width() * imageCount; //determines the end</p>
<p> if (position <= 0) //cycle once the beginning is reached
{
position = rotationNumber - $('#imgGal').width();
}
else{
position -= $('#imgGal').width();//position change to width of the element
}
//jquery to animate the transition
$( ".rotator" ).animate({
right: position
}, 1000 );
}
</script><p></p>
<div id="testimonials">
<a class="controls" onclick="previousImage()"><p></p>
<div class="arrow"><</div>
</a><p><a class="controls" onclick="previousImage()"></a><!--
--></p>
<div id="imgGal">
<div class="rotator">
<div class="blurb">
<h3>John Doe</h3>
<p>"Positive Testimonial"</p>
</div>
</div>
<p><!--
--></p>
<div class="rotator">
<div class="blurb">
<h3>John Doe the Second</h3>
<p>"Positive Testimonial"</p>
</div>
</div>
</div>
<p><!--
--><a class="controls" onclick="nextImage()"></a></p><a class="controls" onclick="nextImage()">
<div class="arrow">></div>
</a><p><a class="controls" onclick="nextImage()"></a>
</p></div>
</div>
Notice the doubling of elements and insertion of random p tags? I haven't been able to find anything on this so I'm kind of stumped. If I isolate the code it's happening on and just throw it in a non-wordpress HTML page by itself the issue doesn't happen. It's happening specifically on the a tags that I'm using to trigger the javascript
The code that is actually having the issue
Goes from this
<a class = "controls" onclick="previousImage()"><div class = "arrow"><</div></a>
<a class = "controls" onclick="nextImage()"><div class = "arrow">></div></a>
to this
<a class="controls" onclick="previousImage()"><p></p>
<div class="arrow"><</div>
</a><p><a class="controls" onclick="previousImage()"></a><!--
--></p>
<a class="controls" onclick="nextImage()"></a></p><a class="controls" onclick="nextImage()">
<div class="arrow">></div>
</a><p><a class="controls" onclick="nextImage()"></a>
As #janh2 has stated. This is most likely down to 'wpautop' - https://codex.wordpress.org/Function_Reference/wpautop
You can disable it by adding the following code:
remove_filter( 'the_content', 'wpautop' );
or
remove_filter( 'the_excerpt', 'wpautop' );
or both...
I have to ask though, why are you adding JavaScript in to a WordPress page content? It's not the recommended way to add JavaScript code to a WordPress site and it should be enqueued as per the documentation in the WordPress Codex:
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
I try to use two if statement into foreach such as this but in second if statement get error.Please advice
#{
string path = System.Configuration.ConfigurationManager.AppSettings["ImageEdit"];
int i = 0;
}
#foreach (var item in Model.PhotoTables)
{
if (i < 1)
{ <div class="row">
}
<div class="col-xs-2 col-wrapper">
<div class="image-wrapper">
<img src="#Url.Content(path+item.PhotoName)" alt="" />
<img class="delimg" src="~/Content/Adminex/images/delete-icons.png" id="#item.Id" />
</div>
</div>
#if (i < 1 )
{
</div>}
</div>
i++;
}
You seem to have an extra closing div with no matching start tag.
Your code block can contain all of the logic. Try:
#{
string path = System.Configuration.ConfigurationManager.AppSettings["ImageEdit"];
int i = 0;
foreach ( var item in Model.PhotoTables )
{
if ( i < 1 )
{
<div class="row">
}
<div class="col-xs-2 col-wrapper">
<div class="image-wrapper">
<img src="#Url.Content(path + item.Name)" alt="" />
<img class="delimg" src="~/Content/Adminex/images/delete-icons.png" id="#item.Id" />
</div>
</div>
#if (i < 1 )
{
</div>
}
i++;
}
}
You only need the # symbol on the containing codeblock.
The # symbol is not required when you directly nest actual code blocks (not HTML), so the first foreach, and if are ok, but then we go into HTML, and then back to code for the second if - we need to use a #.
Manually manipulating a variable inside a foreach statement can possibly lead to trouble. Instead, take advantage of current framework method Select().
Model:
var model = new SampleViewModel
{
Items = new List<string>
{
"A",
"B",
"C"
}
};
return View(model);
}
Razor:
#foreach(var item in Model.Items
.Select((value, index) => new { Value = value, Index = index }))
{
if ( item.Index < 1 )
{
<div class="row">
}
<div>#item.Index #item.Value</div>
if ( item.Index < 1 )
{
</div>
}
}
Results:
0 A
1 B
2 C
DotNetFiddle MVC Example
I'm using mustache.js as my template system, I'm not very experienced with logic-less
templates so I decided to ask you this question.
I have the following html structure, two rows with 3 columns each, like so:
<div class="content-wrapper">
<div class="grid-row">
<div class="column-third">
<p>Text</p>
</div>
<div class="column-third">
<p>Text</p>
</div>
<div class="column-third">
<p>Text</p>
</div>
</div>
<div class="grid-row">
<div class="column-third">
<p>Text</p>
</div>
<div class="column-third">
<p>Text</p>
</div>
<div class="column-third">
<p>Text</p>
</div>
</div>
</div>
What I have at the moment on my template is the following
<div class="content-wrapper">
<div class="grid-row">
{{#contentListToRender}}
<div class="column-third">
<p>{{title}}</p>
</div>
{{/contentListToRender}}
</div>
</div>
But as you can already guess that will fill the row with as many elements (columns)
I have in contentListToRender.
I was wondering if there's a way of doing this and still keep the html structure,
or I should start thinking of chaning the html structure and use CSS to style the appearance?
Thank you for your help,
David
Edit, followed pawel comment.
I assume you mentioned to do something like this?
<div class="content-wrapper">
{{#contentListToRender}}
{{#rows}}
<div class="grid-row">
{{#columns}}
<div class="column-third">
<p>{{title}}</p>
</div>
{{/columns}}
</div>
{{/rows}}
{{/contentListToRender}}
</div>
Being my contentListToRender composed this way:
var contentListToRender = function() {
return {rows: [ { columns: [column, column, column] }, {columns: [column, column, column]} ]}
}
Would that be an acceptable way of accomplish this?
The following is the solution I build for this, in case someone ends up in the same place as me.
<div class="content-wrapper">
{{#contentListToRender}}
{{#rows}}
<div class="grid-row">
{{#columns}}
<div class="column-third">
<p>{{title}}</p>
</div>
{{/columns}}
</div>
{{/rows}}
{{/contentListToRender}}
</div>
And the function to split the elements into rows and columns
var contentListToRender = function(elements) {
var rows = { rows: [] };
var columns = [];
for (var i = 0; i < elements.length; i++) {
var base_path = elements[i].base_path;
var title = elements[i].title;
var column = {base_path: base_path, title: title};
if ((columns.length > 0) && (columns.length % 3 == 0)) {
// when the columns array has reached 3 elements
// then add those columns to the rows
rows.rows.push({columns: columns});
// Then clear the columns array
// and push the current column to the columns array
columns = [];
columns.push(column);
} else {
columns.push(column);
}
}
// push the last columns to the last row
rows.rows.push({columns: columns});
return rows;
}
I have the following code, and it displays the listed text and the pagination at the bottom all fine. But when I try to get the images from the child page into the list it errors for me. Any ideas why?
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = "SiteLayout.cshtml";
}
<div role="main" id="main">
<div id="blogPageFeatures">
<!-- loop through all of the child pages (blog posts) and grab data to output as a summary -->
#{
var pageSize =3;
var page =1;int.TryParse(Request.QueryString["page"],out page);
var items =Model.Content.Children().Where(x => x.IsDocumentType("BlogArticle")).OrderByDescending(x => x.CreateDate);
var totalPages =(int)Math.Ceiling((double)items.Count()/(double)pageSize);
if(page > totalPages)
{
page = totalPages;
}
else if(page <1)
{
page =1;
}
}
#foreach(var item in items.Skip((page -1)* pageSize).Take(pageSize))
{
<a href="#item.Url">
<!-- The next image line is where it errors -->
<img src="#Umbraco.Media(item.Children.blogArticleImage).Url">
<h2>#item.Name</h2>
<p class="blogTileDate">#item.CreateDate.ToString("dd/MM/yy")</p>
</a>
}
<div class="clearfix"></div>
<!-- Pagination START -->
#if(totalPages >1)
{
<div class="pagination">
<ul>
#if(page >1)
{
<li>Prev</li>
}
#for(int p =1; p < totalPages +1; p++)
{
var active =(p == page)?" class=\"active\"":string.Empty;
<li#(Html.Raw(active))>
#p
</li>
}
#if(page <totalPages)
{
<li>Next</li>
}
</ul>
</div>
}
<!-- Pagination END -->
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
You are getting Model.Content.Children, which is a strongly typed collection of IPublishedContent objects. You are then calling item.Children.blogArticlImage, which is not going to work, as you're trying to get the a property on the children of the item, not a property of the item itself. You're also trying to use dynamic syntax, which won't work on the strongly typed objects. Try changing your image code to:
<img src="#Umbraco.TypedMedia(item.GetPropertyValue<int>("blogArticleImage")).Url">
That should do te trick, one thing to note is that if an item doesn't have an image set, or the image that has been selected has been deleted, it'll throw an error. A more robust approach would be something like this:
var imageSrc = "ADD DEFAULT IMAGE PATH HERE";
var media = Umbraco.TypedMedia(item.GetPropertyValue<int>("blogArticleImage"));
if (media != null && !string.IsNullOrEmpty(media.Url))
{
imageSrc = media.Url;
}
<mig src="#imageSrc">
I'm new to MVC5 and especially to using Bootstrap. I'm trying to create a website in which the first page should feature a grid with student images. Right now it looks like this. I want to have 5 images per row, but I haven't manage to figure out how(it was either 4, or 6).
Also when there is no space between the rows and the images stuck together.
I am populating the grid with items from my Student Model.
<div class="col-md-10">
<div class="row">
#foreach (var item in Model.Students)
{
<div class="col-md-2">
<a href="#Url.Action("Edit", "Students", new {studentId = item.StudentId})">
<img src="../../#item.ProfileImagePath" alt="Profile Image" />
</a>
</div>
}
</div>
</div>
I didn't fully understand how does the bootstrap grid work, especially when I'm populating it with dynamic data. The website looks like this now
http://imgur.com/5fLRZeM
You have
<div class="row">
#foreach (var item in Model.Students)
{
<div class="col-md-2">
...
</div>
}
</div>
Which generates one long <row>. But there is no auto-wrapping, you'll have to break this up into rows yourself.
Assuming that Students is a List<>
// roughly, untested
#for(int r = 0; r < Model.Students.Count; r += 5)
{
<div class="row">
#for (int s = r; s < r+5; s += 1)
{
<div class="col-md-2">
// do stuff with Model.Students[s]
</div>
}
</div>
}