Dynamically generate multiple less files from less template and database variables

StackOverflow https://stackoverflow.com/questions/23566301

  •  19-07-2023
  •  | 
  •  

Question

I have a less file template with some varables at the top. We are bringing on 500 new clients so I need to generate 500 new less files from the colors that each client is going to give me. These colors will be stored in a database. I tried to accomplish this with an xslt template but all the & keep coming out as & and is breaking the less. This is what I have so far

 //loop through each row of colors
 XmlElement elem = doc.GetElementFromRow(row);
 var xslTran = new XslTransform();
 xslTran.Load(filePathToXslt);
 var fileName = String.Format("theme_{0}.less", row["Name"]);
 var writer = new XmlTextWriter(mappedPath, Encoding.UTF8);
 xslTran.Transform(elem, null, writer);

Less looks something like this.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/NewDataSet">
//variables
@color_link:#<xsl:value-of select="Table/Color3" />; // secondary1
@color_link_hover:#<xsl:value-of select="Table/Color2" />; // Secondary1 hover

#profileTabs {
  .tab {    
    <![CDATA[&#explore-tab:hover {]]>
      .h-type5 {
       h1, h2, h3, h4, h5, h6 {
        background-color: @color_link;
       }
     } 
    }
}
</xsl:template></xsl:stylesheet>

Im looking for a solution to this or if there is a beter way to do it without using xslt please let me know. Thanks in advance.

Était-ce utile?

La solution

CSS and less are no XML formats, so XSLT might not be the droids you are looking for the best tool. I found Mustache quite suitable for such a task. It does have a C# implementation available. Your input file would look something like:

   @color_link:#{{color3}}; // secondary1
   @color_link_hover:#{{color2}} />; // Secondary1 hover

and you would supply a map with the stuff in double curly braces as keys.

Let us know how it goes.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top