My website has two languages and I have a problem with the footer links.
Before my redesign I included different files. One with links pointing to /en/privacy/ or /en/contact/ etc. and another file with links like /datenschutz/ or /kontakt/
<% if Locale == en_GB %>
<% include Footer_en_GB %>
<% else %>
<% include Footer %>
<% end_if %>
That makes it hard to maintain.
Is there a better way? IS it possible to get the propper I18N link for a page?
Something like
i18ntitle('/datencshutz/')
so the EN footer will look like
Privacy
and the DE footer will look like
Datenschutz
If you are using Translatable Module and the Pages are actually Linked in the CMS, you can simply use $Link?Locale=en_US or rather /datenschutz/?Locale=en_US.
When you then visit /datenschutz/?Locale=en_US, SilverStripe will response with a redirect to the EN Link.
Or in PHP, you can use:
if ($page->hasTranslation('en_US')) {
$link = $page->getTranslation('en_US')->Link();
}
PS: this gist might also interest you: https://gist.github.com/Zauberfisch/9226142#file-translatablecontrollerextension-php-L39
I thought there is some simple solution built in SS, but couldn't find any.
Here is what I have done now
Template:
<ul class="vertical menu">
<li>Startseite</li>
<li>Über uns</li>
<li>Kontakt</li>
</ul>
Necessary code in mysite/code/page.php (inside the Page class):
public function LanguageURL($page) {
if (empty($page)) {
$page = $this;
} else {
$SQL_url = Convert::raw2sql($page);
$page = Translatable::get_one_by_lang('SiteTree', 'de_DE', "URLSegment = '$SQL_url'");
}
if ($page->hasTranslation('en_GB') && Translatable::get_current_locale() == 'en_GB') {
$link = $page->getTranslation('en_GB')->Link();
} else {
$link = $page->getTranslation('de_DE')->Link();
}
return $link;
}
Your German website outputs
<li>Kontakt</li>
… and your English website outputs
<li>Contact</li>
Actual output depends on your config, but the point is to output the proper URL for the current locale.
Perhaps anybody needs something similar or has a better solution :-)
Related
I am using code like this to detect the browser and apply a condition to display or not display <label>:
#{
var settings = Model.PartFieldDefinition.Settings.GetModel();
System.Web.HttpBrowserCapabilitiesBase theBrow = Request.Browser;
}
<fieldset>
#{
if ((theBrow.Browser == "IE") && (theBrow.MajorVersion < 10))
{
#:<label>#Model.DisplayName</label>
#://Some Code for inputs
}
else
{
#://Some Code for inputs
}
}
</fieldset>
I have been trying to figure out if I can (and if so, how) I could utilize this logic to detect what path in the website the user is in to display or not display <label>
For example, on any other path I want the label displayed. However, for the pages under ~/Services (the path), I want to be able to hide the label.
It's a little bit more complicated than that. The code is in an editor template that gets used all over the site. If I use CSS in the editor template to "hide" the label I will affect all the pages. I was hoping with the above logic I can avoid that and only apply my logic on certain pages under a path.
Thanks.
Update
I have tried the following:
#{
string CurrentURL = Request.ApplicationPath;
}
<fieldset>
#{
if ((CurrentURL == "Services")) // also tried "/Services"
// rest of code left out for brevity
}
but the labels are hidden on all pages, even outside the path "~/Services". Am stuck on the syntax.
Found answer
I did the following:
#{
string CurrentURL = Request.Url.AbsoluteUri;
}
<fieldset>
#{
if (CurrentURL.Contains("Services"))
}
and it worked. I don't know if anyone can comment, but which would be better to use: Request.Url.AbsoluteUri or Request.Url.ToString() (both of which work)?
I have written a training application with each page/slide of the training workbook as a seperate blade template file named as "page1.blade.php", "page2.blade.php" and so on. Each of these files has content of the kind:
#extends('en/frontend/layouts/training_modulename')
{{-- Page title --}}
#section('title')
Page Title
#parent
#stop
{{-- Page content --}}
#section('pageContent')
<div class="pageContentContainer">
<h2>Page Title</h2>
...
</div>
#stop
This works really well when being viewed page by page within the browser. However I also wish to automatically compile all pages into a PDF document. This is being done via dompdf which works amazingly well when I pass each pages html to it manually. However I wish to condense the #section('pageContent') section of each page into one large section which extends a different layout for passing to dompdf.
Given the above context my question is this:
Is there a method in Laravel's blade parser which would allow me to pass it a blade file and just get the rendered html from a particular section? The below pseudo-code demonstrates what I would like to be able to do.
$pages = array(...); // content of the directory
foreach ($pages as $page)
{
$renderedPage = Blade::render($page);
$title = $renderedPage->title;
$pageContent = $renderedPage->pageContent;
}
Instead of doing the normal return of view
return View::make('page');
You can instead store the view in a string
$view = View::make('page');
So then you can do your code something like this (not tested - but you get the idea):
$pages = array(...); // content of the directory
foreach ($pages as $page)
{
$renderedPage[] = view::make($page);
}
Given the following Html.ActionLink:
#Html.ActionLink(Model.dsResults.Tables[0].Rows[i]["title"].ToString(), "ItemLinkClick",
new { itemListID = #Model.dsResults.Tables[0].Rows[i]["ItemListID"], itemPosNum = i+1 }, ...
Data from the model contains HTML in the title field. However, I am unable to display the HTML encoded values. ie. underlined text shows up with the <u>....</u> around it.
I've tried Html.Raw in the text part of the ActionLink, but no go.
Any suggestions?
If you still want to use a helper to create an action link with raw HTML for the link text then I don't believe you can use Html.ActionLink. However, the answer to this stackoverflow question describes creating a helper which does this.
I would write the link HTML manually though and use the Url.Action helper which creates the URL which Html.ActionLink would have created:
<a href="#Url.Action("ItemLinkClick", new { itemListID = #Model.dsResults.Tables[0].Rows[i]["ItemListID"], itemPosNum = i+1 })">
#Html.Raw(Model.dsResults.Tables[0].Rows[i]["title"].ToString())
</a>
MVCHtmlString.Create should do the trick.
Using the actionlink below you do not need to pass html in the model. Let the css class or inline style determine how the href is decorated.
#Html.ActionLink(Model.dsResults.Tables[0].Rows[i]["title"], "ItemLinkClick", "Controller", new { #class = "underline", style="text-decoration: underline" }, null)
those are the cases that you should take the other path
#{
string title = Model.dsResults.Tables[0].Rows[i]["title"].ToString(),
aHref = String.Format("/ItemLinkClick/itemListID={0}&itemPosNum={1}...",
Model.dsResults.Tables[0].Rows[i]["ItemListID"],
i+1);
}
#Html.Raw(title)
Remember that Razor helpers, help you, but you can still do things in the HTML way.
You could also use this:
<a class='btn btn-link'
href='/Mycontroler/MyAction/" + item.ID + "'
data-ajax='true'
data-ajax-method='Get'
data-ajax-mode='InsertionMode.Replace'
data-ajax-update='#Mymodal'>My Comments</a>
Why doesn't Html.ActionLink work in the below code? This is a page in the app_code folder,
that I am trying to call from index.cshtml
LogOnUserControl.cshtml
#helper DisplayUserControl(){
if (Request.IsAuthenticated ) {
<span>Welcome <strong>#User.Identity.Name</strong>!</span>
<span>[ {#Html.ActionLink("","","")} ]</span>
}
else {
<span>[{#Html.ActionLink("","","") }]</span>
}
}
this is the line of code from index.cshtml. The call itself works, if I remove the Html.ActionLink statements the site loads fine. Is it that you can't use them in a nested page like this? How else can I generate dynamic links?
index.cshtml
#LogOnUserControl.DisplayUserControl()
What's the idea with this action links? Why are you passing empty strings as arguments? I suppose you want to generate SignIn, SignOut links, don't you?
Also if you want to use HTML helpers inside shared helpers that you put in the App_Code folder you will need to pass them as arguments because they are not available:
#using System.Web.Mvc.Html
#helper DisplayUserControl(System.Web.Mvc.HtmlHelper html) {
if (html.ViewContext.HttpContext.User.Identity.IsAuthenticated) {
<span>
Welcome
<strong>
#html.ViewContext.HttpContext.User.Identity.Name
</strong>
!
</span>
<span>[#html.ActionLink("SignOut", "Login")]</span>
}
else {
<span>[#html.ActionLink("SignIn", "Login")]</span>
}
}
and to call the helper:
#LogOnUserControl.DisplayUserControl(Html)
Personally I never use such helpers (the ones you put in the App_Code folder). Can't see any use for them when you have partial views, editor/display templates and Html.Action helpers.
So for example you could define a partial (~/Views/Shared/_LogOnUserControl.cshtml):
#if (User.IsAuthenticated) {
<span>
Welcome
<strong>
#User.Identity.Name
</strong>
!
</span>
<span>[#Html.ActionLink("SignOut", "Login")]</span>
}
else {
<span>[#Html.ActionLink("SignIn", "Login")]</span>
}
which you would include in your layout:
#Html.Partial("_LogOnUserControl")
I have a .html.erb file, with some javascript in it. I would like to do something like this:
var stuff = '<div><%= #ruby_var.title %></div>'
What is the best way to do this? I may be totally off... Thanks.
To safely do this you need to use to_json:
<%= javascript_tag do %>
var stuff = <%= #ruby_var.title.to_json %>;
<% end %>
This will ensure your code does not break if #ruby_var.title has a quote in it.
To include the divs I would do:
<%= javascript_tag do %>
var stuff = <%= "<div>#{#ruby_var.title}</div>".to_json %>;
<% end %>
Note that there are no quotes around <%= %>, to_json takes care of that for you.
<script>
var stuff = '<div><%=escape_javascript #ruby_var.title %></div>'
</script>
in your .html.erb file will work fine.
Ruby can't be run on the client, at least not with specialized browser plugins. Instead, look at using AJAX and RJS to query your Ruby web application from Javascript and insert any text it returns into your page.
Without more information, I don't think you're going to get any better answers. I might be totally off too, because I'm only guessing at what you really want to do.
You can use the javascript_tag function to include Javascript in a .html.erb file.
Example:
javascript_tag "alert('This is a test')"
in your foo.html.erb will return
<script type="text/javascript">
//<![CDATA[
alert('This is a test')
//]]>
</script>
to the browser.