有什么方法可以更改此代码,以免我重复相同的html代码4次?

时间:2018-07-03 12:45:53

标签: php laravel

如您所见,在public static Schema makeSchema(String pathToWsdl) throws ParserConfigurationException, IOException, SAXException, InstantiationException, IllegalAccessException, ClassNotFoundException { // read wsdl document File wsdlFile = new File(pathToWsdl); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document wsdlDoc = dBuilder.parse(wsdlFile); // read namespace declarations from wsdl document, in case they are referred from a schema NamedNodeMap attributes = wsdlDoc.getDocumentElement().getAttributes(); Map<String, String> namespacesFromWsdlDocument = new HashMap<>(); for (int i = 0; i < attributes.getLength(); i++) { Node n = attributes.item(i); if (n.getNamespaceURI() != null && n.getNamespaceURI().equals("http://www.w3.org/2000/xmlns/")) { namespacesFromWsdlDocument .put(n.getLocalName(), n.getNodeValue()); } } // read the schema nodes from the wsdl NodeList schemas = wsdlDoc.getElementsByTagNameNS("http://www.w3.org/2001/XMLSchema", "schema"); Map<String, DOMSource> sources = new HashMap<>(); for (int i = 0; i < schemas.getLength(); i++) { // create a document for each schema and copy the source schema Document schema = dBuilder.newDocument(); Element schemaElement = (Element)schema.importNode(schemas.item(i), true); // add all non-existing namespace declarations from the wsdl node String targetNs = schemaElement.getAttribute("targetNamespace"); for (Map.Entry<String, String> ns : namespacesFromWsdlDocument.entrySet()) { String name = ns.getKey(); String value = ns.getValue(); if (schemaElement.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", name) == null) { schemaElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + name, value); } } // map schemas by their target namespace schema.appendChild(schemaElement); DOMSource domSource = new DOMSource(schema); sources.put(targetNs, domSource); } SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // Create a ResourceResolver that can find the correct schema from the map DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); factory.setResourceResolver(new LSResourceResolver() { @Override public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { Source xmlSource = sources.get(namespaceURI); if (xmlSource != null) { LSInput input = domImplementationLS.createLSInput(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Result outputTarget = new StreamResult(outputStream); try { TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); } catch (TransformerException e) { e.printStackTrace(); } InputStream is = new ByteArrayInputStream(outputStream.toByteArray()); input.setByteStream(is); input.setSystemId(systemId); return input; } else { return null; } } }); // create the schema object from the sources return factory.newSchema(sources.values().toArray(new DOMSource[]{})); } col-home-1元素中,我使用了相同的html代码,但我不喜欢这样,但是,我不确定如何避免重复4次。任何提示将不胜感激。

col-home-4

1 个答案:

答案 0 :(得分:2)

您可以在views文件夹中创建局部文件:

imagesComponent:

@if (!empty($images))
    <div class="{{$class}}">
    @foreach($images as $image)
        <div class='imageContainer'>
            <div class="stickyContainer blackGradient">
                <h1 class='imageTitle'>{{$image->name}}</h1>
                <img class='uploadedImg' src='{{url("storage/uploads/images/thumbnails/".$image->file_name)}}' alt='Random image'/>
                <a class='specialA' href='{{url("image/".$image->id)}}'></a>
                @auth
                <div class='votingContainer'>
                    <a href='#' class='vote {{ ( auth()->user()->votes()->whereImageId($image->id)->first() && auth()->user()->votes()->whereImageId($image->id)->first()->vote == 1 ) ? "liked" : "like" }}' id='{{$image->id}}'></a>
                    <p class='voteCount'>{{ $image->upvotes - $image->downvotes }}</p>
                    <a href='#' class='vote {{ ( auth()->user()->votes()->whereImageId($image->id)->first() && auth()->user()->votes()->whereImageId($image->id)->first()->vote == 0 ) ? "disliked" : "dislike" }}' id='{{$image->id}}'></a>
                </div>
                @endauth
            </div>
        </div>
    @endforeach
</div>
@endif

然后在您的视图中,您将像这样进行操作:

<div class="flex-grid-home">
@php($count = 0)
@foreach($images as $image)
    @if ($count % 4 == 0)
        @php($images1[] = $image)
    @elseif($count % 4 == 1)
        @php($images2[] = $image)
    @elseif($count % 4 == 2)
        @php($images3[] = $image)
    @else
        @php($images4[] = $image)
    @endif
    @php($count++)
@endforeach

@include('imagesComponent', ['images' => $images1, 'class' => "col-home-1"])
@include('imagesComponent', ['images' => $images2, 'class' => "col-home-2"])
@include('imagesComponent', ['images' => $images3, 'class' => "col-home-3"])
@include('imagesComponent', ['images' => $images4, 'class' => "col-home-4"])

</div>