Skip to content

Commit

Permalink
Added 'if_null` filter for Liquid tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalarcon committed Apr 19, 2020
1 parent 274cd00 commit c44f1c8
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/Extensions/Filters/MIISLiquidFilters/IfNullFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Linq;
using System.Collections;

namespace MIISHandler.Filters
{

//Needed by MIIS to add the correct reference to DotLiquid
public class IfNullFilterFactory : IFilterFactory
{
Type IFilterFactory.GetFilterType()
{
return typeof(IfNullFilter);
}
}

public static class IfNullFilter
{
/// <summary>
/// This filter returns the value if it's not null, or the parameter if the value to filter it's null
/// Very useful to use default values. It can be used several times in the same tag, and saves lots of if-else-end Liquid tags
/// </summary>
/// <param name="input">Any parameter</param>
public static object IfNull(object input, object defValue)
{
return input ?? defValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IfNullFilter.cs" />
<Compile Include="UrlFilters.cs" />
<Compile Include="ReverseFilter.cs" />
<Compile Include="ConcatFilter.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
4 changes: 2 additions & 2 deletions src/MIISHandler/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("3.1.0")]
[assembly: AssemblyFileVersion("3.1.0")]
[assembly: AssemblyVersion("3.2.0")]
[assembly: AssemblyFileVersion("3.2.0")]
1 change: 1 addition & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
<Content Include="insertfiles\mixed_md_html.md" />
<Content Include="insertfiles\component01.md" />
<Content Include="insertfiles\inheritance.md" />
<Content Include="if-null-filter.md" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<add key="MIIS:Lang" value="!!DataFromFile ~/_data/langs/en.yml"/>
<add key="MIIS:Logo" value="images/logo.png"/>
<add key="MIIS:Description" value="Default description"/>
<add key="MIIS:Caching" value="0"/><!-- No Caching: Useful for debugging -->
<add key="MIIS:Caching" value="false"/><!-- No Caching: Useful for debugging -->
<add key="MIIS:EnableMDExtensions" value="hardlinebreak+bootstrap+smartypants"/>
<add key="copyright" value="JM Alarcón 2017-2019"/>
<add key="copyright" value="JM Alarcón 2017-2020"/>
<add key="cssfile" value="~/CSS/github.css"/>
<add key="MIIS:TemplateName" value="readthedocs"/>
<add key="MIIS:Layout" value="main.html"/>
Expand Down
62 changes: 62 additions & 0 deletions src/Tests/if-null-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
Title: 'if_null' liquid filter usage
#image: /images/myImage.png
#og-image: /images/myOGImage.png
bg-image: /images/myBGImage.png
---

# 'if_null' filter usage

MIIS adds in 3.2.0 a custom filter called `if_null`. This filter allows you to save if-else-end Liquida tags and make your templates much more concise and readable.

For example, imagine that you have 3 possible images that you can use in a page:

- `image`: the normal if_null field used for blog posts or any other file.
- `bg-image`: a background image that your template design optionally can use as an specific image for the header's background.
- `og-image`: a specific image to be used for social sharing in the Open Graph headers for the page

In the social headers code you want to make the precedence of this images this way:

`og-image > image > bg-image`

So, if `og-image` is not available, use `image` and if `image` is not available either, then use `bg-image`.

In normal Liquid syntax you would probably do something like this:

```liquid
{%- raw -%}
{%- if og-image -%}
{%- capture imgurl -%}{{ og-image | absolute_url }}{%- endcapture -%}
{%-elsif image -%}
{%- capture imgurl -%}{{ image | absolute_url }}{%- endcapture -%}
{%-else bg-image -%}
{%- capture imgurl -%}{{ bg-image | absolute_url }}{%- endcapture -%}
{%- endif -%}
<meta property="og:image" content="{{ imgurl }}" />
{%- endraw -%}
```

>Note: Thanks to MIIS' `absolute_url` filter you save some "ifs" inside the capture taking into account if it's an absolute, relative or relative to the root URL.
Well, with the `if_null` filter, you can now simple write:

```liquid
{%- raw - %}
<meta property="og:image" content="{{ og-image | if_null: image | if_null: bg-image | absolute_url }}" />
{%- endraw -%}
```

and you're done! Much clearer and succint.

You can check it in use in the code for this page. Note that this page has only defined the `bg-image` parameter, therefore in the next paragraph you should see the URL for that image:

`{{ og-image | if_null: image | if_null: bg-image | absolute_url }}`

You can play with it uncommenting the other images in the Front-Matter and see how it behaves.
1 change: 1 addition & 0 deletions src/Tests/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _{{customfield}}_
- [Liquid Syntax in HTML](~/liquid.mdh){target="_blank"}
- [Raw content (text/plain) from MDH](~/raw.mdh){target="_blank"}
- [Custom filters](~/customfilters.md)
- [if_null MIIS filter](~/if-null-filter.md)
- [Custom tags](~/customtags.md)
- [Custom Front-Matter and Request params](~/customFMSrc.md?param1=1&s=Hi)
- [Post list - FileFromFolder custom FMSource](~/posts/)
Expand Down

0 comments on commit c44f1c8

Please sign in to comment.