I have 2 Display Templates that have the same code on a few lines.
Is there is a way to move a common code to another file and reference that file in both templates?
For example:
Display Template #1 and # 2 have the following common code:
IFormatProvider formatProvider = System.Globalization.CultureInfo.CurrentCulture;
string formattedCurrency = null;
if (formatProvider.ToString().Equals("en-US"))
{
formattedCurrency = (@Model < 0 ? @Model.ToString("C", formatProvider) : (@Model).ToString("C", formatProvider));
}
else
{
formattedCurrency = (@Model < 0 ? "- " + @Math.Abs(@Model).ToString("C", formatProvider) : (@Model).ToString("C", formatProvider));
}
How can I move it to a separate file and reference that from both templates?
So my first option was to have the
formattedCurrencyprop as a common function:And then inherit both ViewModels from this base model
Then every time you create an object you have access to this prop:
Now both viewmodels will have the formattedCurrency to display on any view, without the need on any extra view logic. Hope this helps and could be a viable option to you.
And render that property on any template. Again this is an alternative solution to achieve the same results. But with a different approach.
Regards