Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
  <title>Modernizing legacy web frontends</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css">
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script type="text/javascript" src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

  <!-- html structure that was originally build by jQuery in showDetail as EJS template -->
  <script type="text/template" id="heroDetailTemplate">
    <img style="width: 150px; float: right;" src="<%= hero.image %>">
    <h1>Details of <%= hero.name %></h1>
    <p>The secret identity of <%= hero.name %> is <%= hero.secretIdentity %>
    and he first appeared in a comic in the year <%= hero.firstAppearance %>.</p>
    <a class="button jsShowAllHeroesButton" href="#">Show all heroes</a>
  </script>

  <!-- container that contains the rendered web application -->
  <div id="app" class="container">
    <div id="heroList" class="row"></div>
    <div id="heroDetail" class="row" style="display: none;"></div>
  </div>

</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                "use strict";
// this example is part of a blog post:
// https://www.lars-berning.de/modernizing-legacy-web-frontends-adding-templates-part-1

// for demo purposes the data is embedded, usually this would be the result of an ajax fetch or similar
var heroData = [
  { name: "Superman", secretIdentity: "Clark Kent", firstAppearance: "1932", image: "https://upload.wikimedia.org/wikipedia/en/thumb/e/eb/SupermanRoss.png/250px-SupermanRoss.png"},
  { name: "Batman", secretIdentity: "Bruce Wayne", firstAppearance: "1939", image: "https://upload.wikimedia.org/wikipedia/en/thumb/7/74/Batman_Detective_Comics_Vol_2_1.png/250px-Batman_Detective_Comics_Vol_2_1.png"},
  { name: "Flash", secretIdentity: "Barry Allen", firstAppearance: "1956", image: "https://upload.wikimedia.org/wikipedia/en/thumb/b/b7/Flash_v1_105.jpg/250px-Flash_v1_105.jpg"},
  { name: "Green Lantern", secretIdentity: "Hal Jordan", firstAppearance: "1959", image: "https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Green_Lantern_Rebirth_6.jpg/220px-Green_Lantern_Rebirth_6.jpg"},
  { name: "Hulk", secretIdentity: "Robert Bruce Banner", firstAppearance: "1962", image: "https://upload.wikimedia.org/wikipedia/en/5/59/Hulk_%28comics_character%29.png"},
  { name: "Spider-Man", secretIdentity: "Peter Parker", firstAppearance: "1962", image: "https://upload.wikimedia.org/wikipedia/en/thumb/2/21/Web_of_Spider-Man_Vol_1_129-1.png/250px-Web_of_Spider-Man_Vol_1_129-1.png"},
  { name: "Iron Man", secretIdentity: "Tony Stark", firstAppearance: "1963", image: "https://upload.wikimedia.org/wikipedia/en/thumb/e/e0/Iron_Man_bleeding_edge.jpg/220px-Iron_Man_bleeding_edge.jpg"},
  { name: "Wolverine", secretIdentity: "James Howlett", firstAppearance: "1974", image: "https://upload.wikimedia.org/wikipedia/en/c/c8/Marvelwolverine.jpg"}
]

// bootstrap web frontend
function init(){
  renderHeroes();
}

// renders the list of superheroes
function renderHeroes() {
  var heroList = $("#heroList");
  var table = $("<table />").addClass("u-full-width");
  table.append($("<thead><tr><th>Name</th><th>Secret Identity</th><th>Details</th></tr></thead>"));
  var tableBody = $("<tbody />");
  for (var index = 0; index < heroData.length; index++){
    tableBody.append(buildHero(heroData[index], index));
  }

  table.append(tableBody);
  heroList.append(table);
};

// used by function renderHeroes to create the jQuery representation of a superhero
function buildHero(item, index) {
  var tableRow = $("<tr />");
  var nameElem = $("<td />").html(item.name);
  var secretIdentityElem = $("<td />").html(item.secretIdentity);
  var detailLink = $("<a />").attr("href", "#").html("Show Details").click({index: index}, showDetail);
  var showDetailElem = $("<td />").append(detailLink);

  tableRow.append(nameElem).append(secretIdentityElem).append(showDetailElem);
  return tableRow;
};

// renders the detail view of a hero, event contains the index of the selected hero
function showDetail(event) {
  event.preventDefault();
  var hero = heroData[event.data.index];
  var detail = $("#heroDetail");
  detail.empty();

  // get the template as string (defined in html file)
  var heroDetailTemplate = $('#heroDetailTemplate').html();
  // use lodash to compile the template
  var compiledHeroDetailTemplate = _.template(heroDetailTemplate);
  // render the template with the data of a hero and attach it to the DOM
  detail.append(compiledHeroDetailTemplate({hero: hero}));
  // since the template is a plain string we have to add the click listener afterwards
  detail.find(".jsShowAllHeroesButton").click(showHeroes);

  $("#heroList").hide();
  detail.show();
};

function showHeroes(event) {
  event.preventDefault();
  $("#heroDetail").hide();
  $("#heroList").show();
};

// call the init function when the page is loaded
$( init );
              
            
!
999px

Console