This should be something embarrassingly simple, but I can't get it to work: I'd simply like to display an image that was uploaded to the Umbraco Media Library (Umbraco 7.1.1) within a Partial View template. The code is
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
var imgNode = CurrentPage.BannerBackgroundImage;
var imgUrl = umbraco.library.NiceUrl(imgNode);
<div id="banner-wrapper" style="background: url('#imgUrl') center center no-repeat;">
<!-- some irrelevant content -->
</div>
}
where BannerBackgroundImage is a custom property of the page. When this is displayed, however, the #imgUrl gets replaced with #.
Other alternatives that I've tried are multiple Media Picker images, how to display a Media Picker image, get image from media with Razor, and display image from Media Picker, to name but a few.
I'd really appreciate if somebody could help me with what I believe is a rookie question!
I found this way easy and clean:
#if (CurrentPage.Image != null && !(CurrentPage.Image is Umbraco.Core.Dynamics.DynamicNull))
{
var m = Umbraco.Media(CurrentPage.Image);
<img src="#m.Url" alt="#m.UrlName" />
}
I hope that it helps somebody else
Thanks Jesus Mogollon,
I've collapsed that to:
<img src="#Umbraco.Media(CurrentPage.headerBackgroundImage).Url" alt="">
I've set the file to mandatory so hopefully I wont need the if statement part.
Having had such an unexpectedly hard time for something that I would have thought would be easy, this snipped worked for me.
#if (Model.Content.HasValue("OtherImages"))
{
var otherImages = Model.Content.GetPropertyValue<List<IPublishedContent>>("OtherImages");
foreach (var image in otherImages)
{
if (image != null)
{
<img src="#image.Url" alt="#image.Name" class="img-responsive img-rounded" />
}
}
}
Much of the other postings did not work for me, but I think that the API has changed a bit. I'm using Umbraco 7.6.1. I'm not sure whether the null check is necessary, but it certainly won't do any harm.
This worked for me, from the Umbraco 6 Media docs, here
var bannerImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue("plainImage"));
<div class="my-banner-wrapper" style="background-image: url(#bannerImage.GetPropertyValue("umbracoFile"));">
<!-- some irrelevant content -->
</div>
Try this
#if (CurrentPage.Image != null && !(CurrentPage.Image is Umbraco.Core.Dynamics.DynamicNull))
{
var m = Umbraco.Media((int)CurrentPage.Image);
<img src="#m.Url" alt="#m.UrlName" />
}
note: You should cast CurrentPage.Image as int because of Umbraco.Media ambigous constructor
<img src="#Umbraco.Media(Convert.ToString(#Umbraco.Field("image"))).umbracoFile" alt="" />
Inside the template
umbraco:Macro runat="server" language="cshtml"
img src="#Model.MediaById(Model.photo).umbracoFile" alt=""/
/umbraco:Macro
---Model.photo =photo is a alice name
I entered this into the section above the doctype in the template or master being used.
#{
Layout = null;
var regionalPage = Umbraco.Content(this.CurrentPage.Id);
string manangerPhotoUrl = string.Format("https://assets.yourdomain.com{0}", #Umbraco.Media(regionalPage.managerPhoto).Url);
}
Then I added the variable holding the string value to my image source attribute in the markup.
<img class="img-responsive" src="#manangerPhotoUrl" />
Related
Here's my code
#{ var RS = ViewBag.RS;}
#if (!RS.eof)
{
while (!(RS.eof))
{
<text>#RS[0]|#RS[0]||</text>
RS.MoveNext();
}
}
else
{
#:|Sorry...nothing found.||
}
I'm justing wanting the contents of RS[0] |RS[0] on the page and nothing else.
For some reason a new line is being inserted BEFORE the text I want.
I am not sure why this is but I'd appreciate if anyone could help nail this down as it's affected other pages of mine before as well.
Thanks.
Edit: Also, I have this view being returned as a partial view so as far as I'm aware the contents of this should literally be the contents of the cshtml page and no header nonsense.
I am trying to write a decorator that will sit on a <ul> element and apply an "active" class to its children <li> based on the current route that is active.
Is it possible for me to inject the router into the decorator and do something like the following?
class ActiveNavDecorator {
RouteProvider _router;
String _currentRoute;
ActiveNavDecorator(this._router) {
_currentRoute = _router.currentURLPath() // I am looking for something that will do this
}
}
Does anyone know how I can accomplish this?
Thanks,
Jarred
Here's my solution for this problem.
I can only take partial credit, because I researched this a couple months ago and came across a similar solution that I based my solution on. I can't remember who wrote that original solution or where I read it, so… apologies in advance! If the original author sees this and leaves a comment, I will update my answer with an attribution.
Anyway, the first part is a decorator:
import 'dart:html';
import 'package:angular/angular.dart';
import 'package:route_hierarchical/client.dart';
/// This decorator modifies UI elements based on the currently selected route.
///
/// For example, when a menu item is clicked, we add an 'active' CSS class
/// to that element so that the user can see where they are.
#Decorator(selector: '[current-route]')
class CurrentRoute {
Router router;
Element element;
/// Constructor.
///
/// Takes an HTML [element] to monitor and the application's [router]. The
/// element must contain a child <a> element. When the route changes, the
/// anchor href's first path component will be compared to the new route's
/// first path component. If it matches, the CSS class `active` will be
/// added to the element. If the route does not match, then the CSS class
/// `active` will be removed.
CurrentRoute(Element element, Router router) {
this.element = element;
this.router = router;
toggleActive(window.location.href);
router.onRouteStart.listen((e) {
toggleActive(e.uri);
});
}
/// Returns true if the given URI matches the anchor href for this element.
bool isRoute(String uri) {
Element anchor;
if (this.element is AnchorElement) {
anchor = this.element;
} else {
anchor = this.element.querySelector('a');
}
String anchorPath = anchor.pathname.split('/')[1];
String routePath = Uri.parse(uri).path.split('/')[1];
return anchorPath == routePath;
}
/// Set the `active` CSS class on an element when it matches the currently
/// selected route.
void toggleActive(String uri) {
if (isRoute(uri)) {
element.classes.add('active');
} else {
element.classes.remove('active');
}
}
}
The basic mechanism is that every time a new route starts, each element that is decorated with current-route will run the isRoute() logic, which checks to see if the first path component of the current window.location is equal to the first path component of the anchor's href. (E.g. the window.location is /foo/bar/baz and the anchor's href is /foo, then that's a match.)
There are other possible ways to perform this step, of course, and you should customize isRoute() as needed for your use case. This just happened to work well for my use case.
Next, you need to register CurrentRoute with Angular's DI. That's left as an exercise for the reader.
Finally, here's an example of the markup to use it, based on a Bootstrap style nav:
<ul class='nav navbar-nav'>
<li current-route>
<a href='/foo'>Foo</a>
</li>
<li current-route>
<a href='/bar'>Bar</a>
</li>
<li current-route>
<a href='/baz'>Baz</a>
</li>
</ul>
Note that the decorator is applied on the element that needs the active class. The decorator will search for the first <a> child of that element if the element is not itself an anchor.
I have a problem in my KnockoutJS application that I can't seem to figure out. Basically, I've bound a list to a 'ko.computed' method which allows me to filter items from the main list. I use this list for my main display to the user. On each item in my template, I have one ore more buttons that I need to render as JqueryUI buttons. I can't seem to find the way to redraw the buttons correctly in my model once the computed triggers a change.
Here is a very (very) simple example of a mock view model:
function List(items) {
var self = this;
self.allItems = ko.observableArray(items || []);
self.search = ko.observable('');
self.filtered = ko.computed(function(){
var search = self.search();
return ko.utils.arrayFilter(self.allItems(), function(item){
return item == search;
});
});
}
My view might look like this:
Search: <input type='text' data-bind='value: search' />
<ul data-bind='foreach: filtered'>
<li>
<span data-bind='text: $data'> </span>
<button>NOTICE</button>
</li>
</ul>
And here is how I initialize the display:
$(function(){
var vm = new List(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
ko.applyBindings(vm);
$('button').button(); // <-- notice!
});
Note that everything works fine initially! I get the nice looking JqueryUI button when the page first displays... However, as soon as I enter a into the search box, the button loses it's style completely. I need to find a way to call $('button').button() again.
Is there an event or callback inside of Knockout.js that I could call to setup my ui buttons after the computed method is triggered?
Thanks in advance!
The reason the style is getting reset is because the dom element that the button was previously bound to has been destroyed.
You can solve this by creating a simple custom binding (not-tested)
ko.bindingHandlers.uibutton = {
init: function(element, valueAccessor) {
var $element = $(element), config = valueAccessor();
$element.button();
}
}
This can be added to your template with this addition
<button data-bind="uibutton: {}">NOTICE</button>
You can remove the call to $('button').button();
When using KO we can almost do without standard Jquery expressions altogether, often custom bindings allow us to do the same but with the possibility of more advanced things like reacting to observables etc.
Hope this helps
I've got:
<th>First Name<span class="ui-icon ui-icon-arrowthick-1-s"></span></th>
When the user clicks on a th cell, I need to clear the span tag from every one of it's siblings.
$('th').click(function() {
var $th = $(this);
$th.siblings().each
Your question isn't very clear -- your title says remove the class, but your post says you need to clear the span tag? To remove the class you would use removeClass() in your each block. Otherwise to remove the entire tag you could use remove().
Edit
You could try this:
$('th').click(function() {
var $th = $(this);
$th.siblings().each(function() {
$('span').remove();
}
});
I have the following scenario:
I have a button\link with a image inside like this:
<button type="submit" id="myButton" class="button"><img src="../../Content/images/check.png" id="defaultImage" />
SaveData!!!</button>
We are OK here! Now what I need to do is:
I want on the click that the image change for a loading element that is previously loaded in the page like this:
<img id="loadingImage" src="../../Content/images/loader.gif" style="display: none;" alt="loading"/>
And then when the load complete turn back the old button image, I ended with this code:
function loader() {
var $button = $('#myButton');
if (btnState == '1') {
$button.find('img').hide();
$button.prepend($('#loadingImage'));
$('#loadingImage').css({ 'display': 'inherit' });
btnState = '0';
}
else {
$button.find('img').hide();
$button.prepend($('#defaultImage'));
$('#defaultImage').show();
btnState = '1';
}
}
This does the trick for ONE SINGLE button(since I pass its ID in the function) but, when I have for example a grid with a button on each line, I found inviable when managing a screen with many buttons do this for each of then. The main question is: How can I make this method general for all buttons/links on one specific class in the page?
The goal is: Click a button, get the image and change it and stop(can be manual). I just don't wanna have to Hook ALL buttons.
You should do something like this, it will prevent the user from double submitting:
$('.button').click(function(evt) {
evt.preventDefault();
var button = this;
hideButton(button);
$('ID or CLASS').load('AJAX FILE URL', function() {
//On success, show button remove image
showButton(button);
});
});
function hideButton(button) {
$(button).hide().after('<img src="../../Content/images/loader.gif" alt="loading"/>');
}
function showButton(button) {
$(button).next('img').hide();
$(button).show();
}
All of the code above should be in the $(document).load
Your HTML should look like:
<button type="submit" class="button"><img src="../../Content/images/check.png" />SaveData!!!</button>
There is no need for Id's now on anything.