When coding xaml we have wonderful resource dictionaries, styles, user controls, and templated controls to make xaml extensible and easy to reuse.
But what about including the same chunck of xaml code multiple places?
Let me give an examle.
I have a grid definition I want to use on all my xaml pages.
<!--Row definitions-->
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
As things are now, I simply copy paste this definition on all my xaml pages. As you can imagine, adding even the simplest of changes to the grid definition becomes a nightmare. It's anything but DRY code.
So, is there a smarter alternative? Like defining the block in a seperate xaml file, and then write something like
<includes:GridDefinitions>
In PHP you can get template tools like Twig and Blade, where you write a master template, and then extend it (Example below is from twig)
base.html.twig
{% block content %}
{% endblock %}
page.html.twig*
{% extends "base.html" %}
{% block content %}
Content of the page...
{% endblock %}
They even have an include statement, which simply takes all the content of one html file and includes it wherever you put the tag
{{ include('page.html') }}
So here comes the question
Does xaml have some sort of template engine or include function that I don't know of yet? Or is there a way to use resource dictionaries etc, to achieve a similiar DRY code effect?
If what I'm looking for is unclear, feel free to post a comment and ask for more information.
From my experience there is not direct answer to your question, but here are some options you can consider:
For parts of UI, stiles,... -- define UserControls, dictionaries, ContentConrols... in separate files as you mention in first line. (Just in case, if you want to "stylize" things like RowDefinitions you should wrap them into Dependency Objects - here is a detailed article.)
For similar pages -- define Visual Studio Templates to generate predefined markup/files, etc.
For similar parts of code -- define code snippets - here is how-to for XAML. They will work at coding-time.
Related
So I am wondering how I can change the image under an accordion section for just one product page and not the others. I see in the code of the product page template where the image is located but I am not experienced enough to know how to change it for just the one product. Is there a way to do this without needing to create a whole new template?
Website: https://auntethels.com (I'm trying to change the image under "heating instructions" for just the Lentil Chili Pot Pie product page)
Not sure if it helps but here is the code snippet I found: 1
(I need the 'directions.png' to be different for just the one page.)
I've been able to make other changes she needed but this one has stumped me :( I tried to reach the developer who made the custom site but he has stopped replying so I'm on my own for this one.
you can conditionally render the accordion with product handle
{% if product.handle == 'cumin-lentil-pot-pie-vegetarian'%}
<div class="accordion">
<h4>Heating Instructions</h4>
<button class="x">✕</button>
<p class="answer"><img src="//cdn.shopify.com/s/files/1/0332/6997/3128/t/15/assets/directions.png?v=63874448465039500411650314323" alt="Directions"></p>
</div>
{% endif %}
I am completly miffed. I'm new to using liquid but not new to programming, it's my day job. Below we have a snippet of the product-template.liquid file. Hopefully, it looks familiar to folk rather than me pasting the whole file. That being said, here's a link to a paste of it.
{% else %}
<div class="product-description rte" itemprop="description">
{{ product.description }}
</div>
{% endunless %}
<div id="shopify-product-reviews" data-id="{{product.id}}">{{ product.metafields.spr.reviews }}</div>
I wanted to add a footer onto descriptions as there's some copy pasted text at the bottom of every product description.
So I changed the above to the the following:
{% else %}
<div class="product-description rte" itemprop="description">
{{ product.description }}
</div>
{% endunless %}
<p>Here's my footer text.</p>
<div id="shopify-product-reviews" data-id="{{product.id}}">{{ product.metafields.spr.reviews }}</div>
This seemed to do the job until I noticed the duplication of the review sections. The content of the <p> tag was in between the two review sections prior to this screenshot:
I removed the <p> tag so that it was back to its original state. However, the review duplication is still there.
Inspecting the page shows one review section nested in the product description div and another one outside of it.
To make matters worse, this issue doesn't seem to be repeatable as far as I can see. I've looked at a product where there is the duplication and another product where there isn't duplication, yet no difference as far as I can see.
Either I'm completely blind in what I returned the code too or maybe Shopify takes time to propagate these changes to templates to each product?
It's worth mentioning, maybe, that I did these code changes using their site code editor.
Update 1
I've worked out the repeatable part. I'm making these changes whilst my partner is creating her products for the site. By my estimations the code changes I made and the products she's added after that point have the review duplication. It seems to be that products prior do not exhibit the duplication issue.
Update 2
Update 1 might not be the case. I duplicated a product where there was not a duplicate review section. The duplicate also showed that there was just one review section. Time of creation doesn't seem to have an impact.
The UWP XAML ItemsControl is the basis for many complicated XAML classes, like ListView and GridView.
The documentation Item containers and templates describes 2 key parts of these controls:
Data template
Control template
These parts combine to create the final view:
Container controls (such as ListViewItem and GridViewItem) consist of two important parts that combine to create the final visuals shown for an item: the data template and the control template.
In practice, developers specify the data template by specifying a DataTemplate in ListView.ItemTemplate (or GridView.), and they can customize the control template by providing a Style (TargetType="ListViewItem") to ListView.ItemContainerStyle.
Developers can also customize the ListView.ItemsPanel (which is an ItemsStackPanel by default), and the default Template for the ListView.ItemContainerStyle contains a ListViewItemPresenter. The documentation for Item containers and templates mentions these, too.
That raises the question:
When I add a ListView (or GridView or any ItemsControl) to my code, what am I actually adding? What can I customize? How is my data displayed?
As far as I can determine, the ListView looks something like:
ListView
Renders its Template which somehow renders:
ItemsPanel
Renders its ItemsPanelTemplate which is:
ItemsStackPanel
Renders, for each item:
ListViewItem
Renders its Template, which is:
ListViewItemPresenter
Somehow renders:
ListView.ItemTemplate
But this is unclear to me.
Disclaimer: I work for Microsoft.
you can customize anything in xaml
under the hood, both ListView and GridView can be created by ItemsControl, but their default template have some customization built-in already.
if you want to understand when to use which, here is a page:
https://learn.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/lists
your data will be set to the ItemsSource if using binding, for example
<ListView
ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
>...
or you can set directly.
from your question, it sounds like you may not have understood the basis, so maybe reading this series will help you:
http://drwpf.com/blog/itemscontrol-a-to-z/
it's for wpf, but the fundamental is the same, you can apply it to UWP as well.
Transifex is widely used to manage translation for Django projects, as well as internally. It does very well on gettext (PO) files. Django templates, however, are not one of its known formats. You can tell Transifex that they are HTML, in which case it takes something like this:
{% extends "base.html" %}
{% block "banner" %}
<h1>Hi there</h1>
<p>Banner text</p>
{% endblock "banner" %}
and turns it into:
<div>
<p>
{% extends "base.html" %}
{% block "banner" %}</p>
<h1>Hi there</h1>
<p>Banner text</p>
{% endblock "banner" %}</div>
An additional problem is that Transifex then treats the template markup as strings to be translated, forcing you to go through the file and tag each one as "locked" - and even then, I think that the markup counts against your word count.
Adding HTML comments around the Django template tags doesn't work either, because they become part of the template and get inserted into the final document. Commenting out template tags and then post-processing the file to remove them might work, but there's not necessarily any guarantee that Transifex would leave the comments alone, and, even if they did, this seems clunky and error-prone.
I'm hoping that someone has a better strategy that lets you hand a template to Transifex without it getting munched.
You should rather use localization support in Django and then you will get Gettext PO files for translation, which can be handled by any localization tool.
Ok, so I'm building an app using Dojo 1.8, and I have a custom widget with a template similar to this...
<div>
<label for="tag">Select something: </label>
<select id="tag"
data-dojo-attach-point="tag"
data-dojo-type="dijit/form/Select">
<option value="0">option 0</option>
<option value="1">option 1</option>
</select>
</div>
However, when the template gets rendered, the widget defines a new id, which makes the tag useless. I've tried googling this, but all my searches just direct to the Dojo documentation since they have attributes called labels but have nothing to do with the HTML label tag.
What is the proper why to do this?
In the situation you describe, you can simply place the label around your <select> and dispose with the for/id attributes. see Stackoverflow question:
How do I align two dojo widgets next to each other?, also see: w3 tutorial on label use
Also, if you want to actually use Ids in a widget template, see:
How do I create unique IDs in a Dojo widget template?
Using ids directly (ie. hard-coding them, not assigning them on-the-fly as in the above link) is not encouraged. The reason for this is that a template is meant to used over and over again in the creation of widgets.
In theory, it could be used to create multiple widgets on one page. Hence, in that situation you would have an id conflict. Each HTML id, on any one page, needs to be unique.