How can I get a field? - umbraco

I'm trying to create a partial view macro that list all items (blog entries). I can read its name, but not the entry field like it's content:
#foreach (var page in CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc"))
{
<div class="article">
<div class="articletitle">#page.Name</div>
<div class="articlepreview">
#Umbraco.Truncate(#page.Field("pageContent"),100)
Read More..
</div>
</div>
<hr/>
}
All pages are defined as a ContentPage (document type) where I've added Page Content (pageContent), type: Richtext editor as a Tab: Content element.
Do I need to cast the page or something to Contentpage?
What Im trying to do Is to give a 100 char long preview of the content on my main page so the users can read a short excerpt before clicking on the item.

Technically they are Properties of a doctype, not Fields.
So I believe this is what you are looking for:
#page.GetProperty("pageContent").Value
And truncate...
#Library.Truncate(#page.GetProperty("pageContent").Value,100)
However in this context you should be able to simply use…
#page.pageContent
So...
#Library.Truncate(#page.pageContent,100)
…should work!

I think this will give you the text you are looking for (I think your problem was just that extra #):
#Umbraco.Truncate(page.Field("pageContent"),100)
If you are running on umbraco 7+, try this:
#Umbraco.Truncate(page.AsDynamic().pageContent, 100)
Hope this helps!

Related

Umbraco 7 - How to allow users to add new content fields on-the-fly?

I'm setting up Umbraco 7.7 for the first time and creating the document types and templates for a page that displays the people that work at our organization (includes their names, photos, and bios).
How do I configure it such that the content manager can add another "person"—effectively a cluster of divs with user-editable images and text—without having to manually add another "person" to the template? Using Partial Views seems like part of the solution, but I'm unclear on how to fit it all together.
My template (simplified) currently looks something to the effect of:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = null;
}
<!doctype html>
<html>
<body>
<div class="person-bio">
<img src="/media/person-01-photo.jpg">
<p>#Umbraco.Field("person-01-name")</p>
<p>#Umbraco.Field("person-01-title")</p>
<p>#Umbraco.Field("person-01-bio")</p>
</div>
<div class="person-bio">
<img src="/media/person-02-photo.jpg">
<p>#Umbraco.Field("person-02-name")</p>
<p>#Umbraco.Field("person-02-title")</p>
<p>#Umbraco.Field("person-02-bio")</p>
</div>
<div class="person-bio">
<img src="/media/person-03-photo.jpg">
<p>#Umbraco.Field("person-03-name")</p>
<p>#Umbraco.Field("person-03-title")</p>
<p>#Umbraco.Field("person-03-bio")</p>
</div>
<!-- etc., etc. -->
</body>
</html>
Thank you! Any help would be much appreciated.
You'll probably will want to use the Nested Content control for this. It allows you to add a list of entities (in your case persons) on a document
More documentation about the nested content control can be found here: https://our.umbraco.com/documentation/getting-started/backoffice/Property-Editors/Built-in-Property-Editors/Nested-Content
So from my understanding you don't need a partial view. If it's that simple and you want to output only the div I see you're repeating, then loop it:
#foreach (var person in whateverYourCollectionIs) {
<div class="person-bio">
<img src="/media/person-01-photo.jpg">
<p>#person.GetPropertyValue<string>("pseudoNameFieldAlias")</p>
<p>#person.GetPropertyValue<string>("pseudoTitleFieldAlias")</p>
<p>#person.GetPropertyValue<string>("pseudoBioFieldAlias")</p>
</div>
}
That loop will create the exact same html for each person, but with the appropriate names, Titles, Bio etc. This is not the actual code you get to use but it hopefully leads you to the correct direction.
This is the documentation that will help

Orchard CMS: How to display a content-item field inside a separate widget?

I'm new to Orchard CMS, and have mostly been dealing with creating a custom theme so far. I've run across an issue and I'm not sure I even know the right concepts yet for what I want to do... but here goes:
I would like to do the following:
Add a custom field to the "Page" content type, called "Parent Title."
On all pages, display the content of the Parent Title field inside the "Before Main" widget zone.
So an author who creates a new page in the CMS will fill out the fields, for example:
Page Title: "Company ABC is Hiring"
Parent Title: "Careers at Company ABC"
etc.
And the rendered markup from Orchard will look something like this (slightly simplified for brevity):
<div id="layout-before-main">
<div class="zone zone-before-main">
Careers at Company ABC
</div>
</div>
...
<div id="content">
<div class="zone zone-content">
<article class="page content-item">
<header><h1>Company ABC is Hiring</h1></header>
<p>Some page content.</p>
</article>
</div>
</div>
I know how to create the custom field; what I don't know is how (or whether) it's possible to put a reference to that field's value inside another zone.
Any advice is appreciated - I am eager to learn!
Is there any reason why you would want to place it in a separate widget? You can just use placement.info for this functionality.
Say you have a TextField attached to the Page content type that is named "ParentTitle". Your placement.info would then look like the following:
<Placement>
<Match ContentType="Page">
<Place Fields_Common_Text-ParentTitle="/BeforeMain:5" />
</Match>
</Placement>
Notice the preceding slash at the BeforeMain. The preceding slash means that it targets a global zone (a layout zone).
If you want to get a grasp on placement.info and which fields/names you should target, enable the module "Shape Tracing". This enables you to look at the fields on the frontend and see which id's to target.
You can read the following articles to understand what is going on:
http://docs.orchardproject.net/Documentation/Customizing-Orchard-using-Designer-Helper-Tools
http://docs.orchardproject.net/Documentation/Understanding-placement-info

Escape # in Html.Raw

I need to parse some HTML from the server side. So I've used
#Html.Raw(MyFunction("key"))
MyFunction returns me the below value
<!--some HTML -->
<li>
Please click the link here to see the Changes to Your Screens.
</li>
<!--some other HTML -->
I am expecting the anchor tag to be rendered like,
Changes to Your Screens
but it is rendered instead as below.
Changes to Your Screens
I have already tried
##(MySiteSettings...
,
#:(MySiteSettings...
and
#:##(MySiteSettings...
For escaping '#' in Html.Raw, but no luck.
#Html.Raw is doing what it's designed to do: This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML. I don't have visual studio now, so please use this as a sample and update accordingly
Based your code, i would recommend you to use PartialViews
Somewhere in your current view:
#Html.RenderAction("PartialList");
PartialList ActionResult
[OutputCache(Duration = 60, VaryByParam = "None")]
public ActionResult PartialList()
{
...
//form up your dynamic <a> here...
return PartialView(<pass in your string here>);
...
}
PartialView
#Html.Raw(Model)
<li>
Please click the link here to see the Changes to Your Screens.
</li>
Then you might ask, why do you have to go through all this trouble? Take a look at this
Not mentioned in that answer is the OutputCache which you can leverage on easily.
Define your function like this:
#helper MyFunction(string key)
{
// do your code here, also with #expressions
<li>
Please click the link here to see the Changes to Your Screens.
</li>
}
I came up with this solution.
#Html.Raw(string.Format(MyFunction("key"), MySiteSettings.DocumentURL + "Documents/MyPDFFile.pdf"))
And I replaced the content in my DB as below for the above to work.
<!--some HTML -->
<li>
Please click the link here to see the Changes to Your Screens.
</li>
<!--some other HTML -->

How to loop through properties in a tab and display them using Razor?

I have a Document Type, that has a tab with some properties.
The properties are Upload types, and Simple Editor types.
(Users are supposed to upload images with some image text).
I have not grouped the "Upload" and "Simple Editor" properties, so how do i do this?
Next question,
I want to loop through each group (there should be 3 currently) and display them on my website.
The markup should look like the following:
<div>
<img src="PATH-TO-UPLOAD-TYPE" />
<div>"TEXT FROM SIMPLE EDTIOR TYPE"</div>
</div>
..
<div>
<img src="PATH-TO-UPLOAD-TYPE" />
<div>"TEXT FROM SIMPLE EDTIOR TYPE"</div>
</div>
...
I would like to use Razor for this. Thanks in advance!
For the first part, using the Razor model, you can't. The content object that you get on the front end only contains the properties, the tabs are not included, as they're only really for organising things in the back office.
You CAN get that information using the Umbraco API, but it's pretty database intensive and could potentially be quite slow if you have a lot of properties/tabs.
You'd be better grouping them yourself in your Razor Macro.
for the second part, you can acces the properties of a page via #Model.property. For example:
<div>
<div>#Model.simpleProperty</div>
</div>

Using RenderPartial twice for same mvc view control in one page, get problems

I'm using same partial view in create.aspx view page twice, part of my code is:
<%Html.RenderPartial("LocationVerifyControl"); %>
<%Html.RenderPartial("LocationVerifyControl"); %>
In the mvc view user controls, it had a button and test code for it:
$('#btnVerify').click(function() { alert("clicked by btnVerify");}
I watched the warning dialog pop up twice when clicked the button, the main view page had contents with same ids. How to solve this kind of problems? To prevent same ids in main view page, put my using html ids in the view data and pass them to partial view?
If I understand your question correctly, you are looking to add unique functionality to two different buttons with the same id.
It's not really valid to have more than one element with the same id. If there will be multiple you should just make it a class. You might want to first run your markup through W3C Validation.
Second you can do this by wrapping the partials in a div or whatever element you like. Something like this:
<div id="first">
<% Html.RenderPartial("LocationVerifyControl"); %>
</div>
<div id="second">
<% Html.RenderPartial("LocationVerifyControl"); %>
</div>
Then your script section can reference the buttons independently.
$('#first #btnVerify').click(function () {
alert('first button click');
});
$('#second #btnVerify').click(function () {
alert('second button click');
});
Notice that I'm just saying to find the id=first then find a id=btnVerify within the id=first.

Resources