I have the following code to display modal windows that I want to use in my project.
No problem, just that I have to include more than 40 modals, this makes my file exceed three thousand lines.
They are basic Bootstrap 3 modals, both the code of the modals and the code of the tags that open them are repeated n<a>
times , the only thing that changes are the ID or href and the internal content of each modal.
<!--Mapa1-->
<div class="col-sm-4 text-center">
<div class="blog-inner">
<a href="#modal1" data-toggle="modal"><img class="img-responsive" src="https://picsum.photos/400/200" alt="Mapa"></a>
<div class="desc">
<h3><a href="#modal1" data-toggle="modal">Sede Colombia</a></h3>
</div>
</div>
</div>
<!--Mapa2-->
<div class="col-sm-4 text-center">
<div class="blog-inner">
<a href="#modal2" data-toggle="modal"><img class="img-responsive" src="https://picsum.photos/400/200" alt="Mapa"></a>
<div class="desc">
<h3><a href="#modal2" data-toggle="modal">Sede Seul</a></h3>
</div>
</div>
</div>
<!-- Modal 1-->
<div class="portfolio-modal modal fade" id="modal1" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-content">
<div class="close-modal" data-dismiss="modal">
<div class="lr">
<div class="rl">
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="modal-body">
<h2>Sede Principal Colombia</h2>
<hr><br>
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d12420.601524132726!2d-77.0365298!3d38.8976763!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x715969d86d0b76bf!2sCasa+Blanca!5e0!3m2!1ses!2sco!4v1543869223728" width="800" height="400" frameborder="0"
style="border:0;width: -webkit-fill-available;" allowfullscreen></iframe>
<br><br>
<hr>
<p><b>Dirección:</b> Bogotá - Colombia
<br>
<b>Teléfonos:</b> (2) 123 4567
</p>
<button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Cerrar</button>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Modal 2-->
<div class="portfolio-modal modal fade" id="modal2" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-content">
<div class="close-modal" data-dismiss="modal">
<div class="lr">
<div class="rl">
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="modal-body">
<h2>Sede Administrativa Seul</h2>
<hr><br>
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d12420.601524132726!2d-77.0365298!3d38.8976763!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x715969d86d0b76bf!2sCasa+Blanca!5e0!3m2!1ses!2sco!4v1543869223728" width="800" height="400" frameborder="0"
style="border:0;width: -webkit-fill-available;" allowfullscreen></iframe>
<br><br>
<hr>
<p><b>Dirección:</b> Seul - Corea del Sur
<br>
<b>Teléfonos:</b> (2) 123 4567
</p>
<button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Cerrar</button>
</div>
</div>
</div>
</div>
</div>
</div>
What I want is to debug this code to avoid repeating so much code that it is basically the same.
I have several ideas, the first thing is to use a php with two includes of the code that is repeated, something like this more or less.
/* modal-start.php */
<div class="modal-content">
<div class="close-modal" data-dismiss="modal">
<div class="lr">
<div class="rl">
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="modal-body">
/* modal-end.php */
<button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Cerrar</button>
</div>
</div>
</div>
</div>
</div>
</div>
<!--Mapa1-->
<div class="col-sm-4 text-center">
<div class="blog-inner">
<a href="#modal1" data-toggle="modal"><img class="img-responsive" src="https://picsum.photos/400/200" alt="Mapa"></a>
<div class="desc">
<h3><a href="#modal1" data-toggle="modal">Sede Colombia</a></h3>
</div>
</div>
</div>
<!--Mapa2-->
<div class="col-sm-4 text-center">
<div class="blog-inner">
<a href="#modal2" data-toggle="modal"><img class="img-responsive" src="https://picsum.photos/400/200" alt="Mapa"></a>
<div class="desc">
<h3><a href="#modal2" data-toggle="modal">Sede Seul</a></h3>
</div>
</div>
</div>
<!-- Modal 1-->
<div class="portfolio-modal modal fade" id="modal1" tabindex="-1" role="dialog" aria-hidden="true">
<?php include 'modal-start.php'; ?>
<h2>Sede Principal Colombia</h2>
<hr><br>
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d12420.601524132726!2d-77.0365298!3d38.8976763!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x715969d86d0b76bf!2sCasa+Blanca!5e0!3m2!1ses!2sco!4v1543869223728" width="800" height="400" frameborder="0"
style="border:0;width: -webkit-fill-available;" allowfullscreen></iframe>
<br><br>
<hr>
<p><b>Dirección:</b> Bogotá - Colombia
<br>
<b>Teléfonos:</b> (2) 123 4567
</p>
<?php include 'modal-end.php'; ?>
<!-- Modal 2-->
<div class="portfolio-modal modal fade" id="modal2" tabindex="-1" role="dialog" aria-hidden="true">
<?php include 'modal-start.php'; ?>
<h2>Sede Administrativa Seul</h2>
<hr><br>
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d12420.601524132726!2d-77.0365298!3d38.8976763!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x715969d86d0b76bf!2sCasa+Blanca!5e0!3m2!1ses!2sco!4v1543869223728" width="800" height="400" frameborder="0"
style="border:0;width: -webkit-fill-available;" allowfullscreen></iframe>
<br><br>
<hr>
<p><b>Dirección:</b> Seul - Corea del Sur
<br>
<b>Teléfonos:</b> (2) 123 4567
</p>
<?php include 'modal-end.php'; ?>
I do not think this method is very practical or very orthodox.
I could also do it with Javascript but I'm not sure if I can include all the html code in the inner, something like this:
document.getElementById("demo").innerHTML = " <div class="modal-content">
<div class="close-modal" data-dismiss="modal">
<div class="lr">
<div class="rl">
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="modal-body">";
The point I want to get to is that I don't want to reinvent the wheel, if there is already a function or a parameter that allows me to automate this copy/paste and avoid code repetition, it would be perfect for me.
Thank you very much!
Create an empty modal template. When you need to use it, fill it with the data you need before displaying it. Instead of having 40 modals, you can have in a Javascript variable an array with 40 records that store the information of each modal and simply fill it when you go to display it:
This way you will have a much more manageable code.
If you wanted to create more than one modal at the same time, you could do something similar but using a template tag (
<template>
) and clone and delete, but it would already be more complex and I think you won't need it.A simple example, which I hope you can adapt to your modal:
You must take into account the following:
Generate a template of the modal you need
Create an array of the data you have:
The Buttons you add the id of the array that you need to be displayed through in
data-id
addition to a class to identify which modal buttons you want the modal to be seen in the example usebtn-modal
Finally we capture the click event, update the data and show the modal:
Everything explained returned Code
As an idea, you could make a json tree where you develop the html you want to display on your page.
El html se construye dinamicamente a partir de este arbol json, en el cual puedes añadir, modificar o eliminar elementos, etiquetas, estilos, etc..... Así es como trabajan frameworks javascript avanzados como React, que crean una copia del DOM html en un json en la memoria del navegador.
Puedes cambiar la funcion :
y poner:
Puedes ver como se repite el mismo elemento de arbol las veces que quieras. O puedes añadir los elementos que tu quieras siempre que los añadas a los Switch de las opciones y los estilos.
Dejo también un enlace en StackBlitz